Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: NSImage to CGImageRef and back again?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSImage to CGImageRef and back again?




On 7/9/07, Scott Thompson <email@hidden> wrote:

Your best bet for creating a CGImage from an NSImage is probably to create a CGBitmapContext on some memory that you own. That way you can choose the color space and pixel format that makes the most sense for the kind of image your working with. Then, with that bitmap context you can create an NSGraphicsContext and draw the NSImage. Once you've drawn the image you can use CGBitmapContextCreateImage to create the CGImage.

Thanks. I put together some code using some sample code and some of my own code (I haven't messed with CoreGraphics very much). But I'm still having the same issues. When I first create the CGImage, everything seems to work fine. I can get the height and width of it, push it's contents back into an NSImage, and draw it. At creation time the CGImageRef is working great. But when it comes time to draw the image as part of view's display method, or something similar, the CGImage seems to be corrupt. The height of the CGImageRef is returned to me as 0, the height is some unintelligible number, and it obviously draws as just an empty space. This sounds like a memory related issue to me. CGImageRefs created from a URL using CGImageSourceCreateWithURL work just great and display correctly at display time, so I'm pretty sure it's my code. I'm pasting my code below, so if someone could give it a look over and make sure it's ok... I'll continue poking away at my classes and make sure it's not an issue elsewhere.


-(void)_setImage:(NSImage *)image
{

        [self willChangeValueForKey:IMAGE_KEY];
	Ptr               contextMemory           = NULL;
	size_t            imageSize;
	size_t            bitsPerComponent        = 8;
	size_t            bytesPerPixel           = 4;
	size_t            bytesPerRow;
	CGColorSpaceRef   colorSpace              = NULL;

bytesPerRow = [image size].width * bytesPerPixel;
imageSize = bytesPerRow * [image size].height;
contextMemory = NewPtr( imageSize );
colorSpace = CGColorSpaceCreateDeviceRGB();

CGContextRef cgContext = CGBitmapContextCreate (contextMemory, [image size].width, [image size].height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
NSGraphicsContext *nsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:cgContext flipped:NO];

[NSGraphicsContext setCurrentContext:nsContext];
[NSGraphicsContext saveGraphicsState];
[image drawAtPoint:NSMakePoint(0,0) fromRect:NSMakeRect(0,0,[image size].width, [image size].height) operation:NSCompositeSourceOver fraction:1.0];
[NSGraphicsContext restoreGraphicsState];

CGImageRef cgImage = CGBitmapContextCreateImage(cgContext);

[self setContents: (id) cgImage];
[self setFrame:CGRectMake(0.0f, 0.0f, [image size].width, [image size].height)];
[self setNeedsDisplay];
[self didChangeValueForKey:IMAGE_KEY];
}


As for why I'm in this situation... My app has some image management stuff that is NSImage bound, so I'm frequently having to push NSImage's into a document to be displayed in Core Animation. It would be entirely possible to move my image management code to be CGImage based, but given the amount of UI code attached to that part of the program, I don't think it would be worth it. I was actually in this situation before. When I was targeting Tiger I was using my own OpenGL layering code, and I would have to create CIImage's from NSImage's. For some reason NSImages -> CGImages is giving me more trouble though.

Thanks,
Colin
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Quartz-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >NSImage to CGImageRef and back again? (From: Colin Cornaby <email@hidden>)
 >Re: NSImage to CGImageRef and back again? (From: Scott Thompson <email@hidden>)
 >Re: NSImage to CGImageRef and back again? (From: "Jaime Rios" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.