Re: NSImageRep simple code
Re: NSImageRep simple code
- Subject: Re: NSImageRep simple code
- From: John Horigan <email@hidden>
- Date: Thu, 4 Mar 2010 09:57:08 -0800
It would be a good idea to modify the pixel buffer before calling initWithBitmapDataPlanes. NSBitmapImageRep makes no guarantee that changes to the data planes will be picked up by the NSBitmapImageRep after the call to initWithBitmapDataPlanes. It seems to work with OSX 10.5 and earlier, but Snow Leopard lazily copies the data planes to a CGImage. Once that copy happens, changes to pixel[] will have no affect on aSimpleBitmap.
-- john
On Mar 3, 2010, at 8:54 PM, Martin Beroiz wrote:
> Hello everyone,
>
> I'm trying to understand how NSImageRep works, so I read the docs and came up with this simple code that seems to... do something.
>
> I'm confused about what's actually doing, I only see a big black block in my custom view.
>
> My intention is to make a 2x2 pixels image with different grayscale colors. For example one pixel black, the other white, etc, so I can see if it actually draws the array I'm passing in to NSImageRep.
>
> I'm guessing that the 2x2 pixel image will scale to fit the entire view so I will see the pixels as big squares in the view.
>
> Here's the relevant code:
>
> @implementation PixelsView
>
> - (id)initWithFrame:(NSRect)frame {
> self = [super initWithFrame:frame];
> if (self) {
> pixels = (unsigned char *)malloc(4*sizeof(*pixels));
> aSimpleBitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:&pixels
> pixelsWide:2
> pixelsHigh:2
> bitsPerSample:8
> samplesPerPixel:1
> hasAlpha:NO
> isPlanar:NO
> colorSpaceName:NSDeviceWhiteColorSpace
> bytesPerRow:2
> bitsPerPixel:8];
> pixels[0] = 1;
> pixels[1] = 0;
> pixels[2] = 0;
> pixels[3] = 0;
>
> }
> return self;
> }
>
> - (void)drawRect:(NSRect)dirtyRect {
> NSRect bounds = [self bounds];
> [NSGraphicsContext saveGraphicsState];
> [aSimpleBitmap drawInRect:bounds];
> [NSGraphicsContext restoreGraphicsState];
> }
> @end
>
> The docs say that NSDeviceWhiteColorSpace has pure white at 1.0, so I'm guessing that pixels[0] = 1 will set a white pixel, am I wrong?
>
> Xcode is not throwing any errors, but the view is not displaying what I expect. Since I don't fully understand bitmap images, is there anything obvious that I'm missing?
>
>
>
> Thanks in advance._______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden