On Aug 5, 2010, at 08:45, Brian Postow wrote:

> Given a bunch of bitmap data, and a resolution, how do I create an NSImage 
> (or CGImage or CGImageSource) with the correct resolution?
> 
> Currently, I'm doing:
> 
>       CGDataProviderRef provider= CGDataProviderCreateWithData(NULL, 
> (UInt8*)data, bytesPerRow * length, NULL);
> 
>       [...]
> 
>       NSData* imgData = [NSMutableData data];
>       CGImageDestinationRef dest = CGImageDestinationCreateWithData
>               (imgData, kUTTypeTIFF, 1, NULL);
>       CGImageDestinationAddImage(dest, cgImg, NULL);
>       CGImageDestinationFinalize(dest);
>       NSImage* img = [[NSImage alloc] initWithData: imgData];
> 
> 
> now, it appears that CGImageDestination takes only a NULL argument for the 
> dictionary.. and width and length are in pixels, not points or inches.  At 
> what point in this process am I supposed to add the resolution into this 
> process?

Yes, the CGImage has no resolution, only a pixel size. So you add in the 
resolution at the very end, after creating the NSImage:

        img.size = NSMakeSize (widthInPoints, heightInPoints);

where the dimensions in points are computed from the pixel dimensions and the 
known resolution.

Actually, after looking at the documentation again, doing it exactly this way 
seems slightly risky (if the NSImage is set to do scaling automatically, for 
example). I think what you really should do is to create a NSImageRep from the 
CGImage, the set the NSImageRep 'size' property to the correct size in points, 
then add that image rep to a new "empty" NSImage.


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to