Re: Trying to get a TIFF from a CGImageRef isn't working
Re: Trying to get a TIFF from a CGImageRef isn't working
- Subject: Re: Trying to get a TIFF from a CGImageRef isn't working
- From: Kyle Sluder <email@hidden>
- Date: Wed, 15 Jan 2014 14:56:03 -0800
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@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