• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSImageRep simple code
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSImageRep simple code


  • Subject: NSImageRep simple code
  • From: Martin Beroiz <email@hidden>
  • Date: Wed, 3 Mar 2010 22:54:53 -0600

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

  • Follow-Ups:
    • Re: NSImageRep simple code
      • From: John Horigan <email@hidden>
    • Re: NSImageRep simple code
      • From: Graham Cox <email@hidden>
  • Prev by Date: Re: Responding to kill signals with the run loop
  • Next by Date: Re: Responding to kill signals with the run loop
  • Previous by thread: Re: Debugging problem
  • Next by thread: Re: NSImageRep simple code
  • Index(es):
    • Date
    • Thread