On Wed, Jan 15, 2014, at 02:14 PM, Steve Mills wrote:
> I've been wrestling with this all day and nothing is working. This code:
> 
> NSImage*              nsImage = [[NSImage alloc] initWithCGImage:cgImage
> size:NSZeroSize];
> NSBitmapImageRep*     biRep = [[NSBitmapImageRep alloc]
> initWithCGImage:cgImage];
> NSData*               nsImageData = [biRep
> TIFFRepresentationUsingCompression:NSTIFFCompressionPackBits factor:0];
> 
> Produces this error on the call to TIFFRepresentationUsingCompression:
> 
> CGImageDestinationFinalize failed for output type 'public.tiff'
> 
> I've also tried NSTIFFCompressionNone and NSTIFFCompressionLZW for the
> compression type.

What is the data format of the original CGImageRef? ImageIO might not be
able to write it to TIFF. (It might have too many bits per pixel, or the
values might be in the wrong order, etc.)

> 
> Hmm, after working some more, I was able to get it to work be forcing the
> image rep to copy the pixels rather than simply glomming onto the
> CGImageRef:
> 
> NSImage*               nsImage = [[NSImage alloc] initWithCGImage:cgImage
> size:NSMakeSize(CGImageGetWidth(cgImage), CGImageGetHeight(cgImage))];
> NSBitmapImageRep*      bitmapImageRep;
> 
> [nsImage lockFocus];
> bitmapImageRep = [[NSBitmapImageRep alloc]
> initWithFocusedViewRect:NSMakeRect(0, 0, nsImage.size.width,
> nsImage.size.height)];
> [nsImage unlockFocus];

I'm surprised this works. As mentioned in the 10.6 AppKit release notes
[1], -[NSImage lockFocus] does not cause +[NSView focusView] to return a
view anymore, so -initWithFocusedViewRect: should be a no-op.

The release notes discuss how to convert image data of arbitrary formats
into something you can work with. [2]

--Kyle Sluder

[1] "NSImage, CGImage, and CoreGraphics impedance matching",
<https://developer.apple.com/library/mac/releasenotes/AppKit/RN-AppKitOlderNotes/>

[2] "NSBitmapImageRep: CoreGraphics impedance matching and performance
notes",
<https://developer.apple.com/library/mac/releasenotes/AppKit/RN-AppKitOlderNotes/>
_______________________________________________

Cocoa-dev mailing list ([email protected])

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

This email sent to [email protected]

Reply via email to