Re: Code and Decode a CGContextRef
Re: Code and Decode a CGContextRef
- Subject: Re: Code and Decode a CGContextRef
- From: Alastair Houghton <email@hidden>
- Date: Wed, 28 Nov 2007 17:12:19 +0000
On 28 Nov 2007, at 16:57, Jason Barker wrote:
Anyway, I just wasn't sure if I had to code/decode each property
from the CGContextRef (width, height, bitsPerComponent, bytesPerRow,
dataSize, colorspace, data, etc.) or if there was an Objective-C
type that could just do it all for me.
No no, that's the crazy way to do it :-)... essentially you'd be
inventing your own binary image file format. Since there are a number
of good ones already, and since many of them are supported by the
system, you might as well use one of those.
With regards to the function CGImageDestinationCreateWithData(), I
think I understand what to pass in for the last three parameters it
takes (namely 'type', 'count' and 'options') but I don't know how to
go from a CGImageRef to a CFMutableDataRef. Is this what I'm
supposed to do? And then once I have a CGImageDestinationRef, how do
I code/decode that?
The steps are as follows:
1. Use CGBitmapContextCreateImage() to create a CGImage.
2. Create an NSMutableData object. (NSMutableData is toll-free
bridged to CFMutableData, so you can pass an NSMutableData pointer
everywhere you see a CFMutableDataRef argument. Many of the CFxxx
types that have equivalents with similar NSxxx names are toll-free
bridged; it usually notes this fact in the documentation for both
types.)
3. Create a CGImageDestination using
CGImageDestinationCreateWithData(). For instance:
NSMutableData *data = [NSMutableData data];
CGImageDestinationRef myDestination;
myDestination = CGImageDestinationCreateWithData
((CFMutableDataRef)data,
CFSTR("public.tiff"), 1, NULL);
(You could pass an NSString instead of using CFSTR, but you'd have to
cast to CFStringRef)
4. Add the image to the image destination using
CGImageDestinationAddImage(). You can release the CGImage at this
point.
5. Call CGImageDestinationFinalize() to make Quartz generate the
data. You can then release the CGImageDestination.
6. The NSMutableData object you created now contains image data in the
format you chose. If you used the code above, that means it contains
a TIFF format image.
Note that, if you're saving your data in a bundled format, you may
prefer to save the data into a separate file in the bundle and then to
save a reference to that file in your normal data file. Depending on
how you're storing your other data, this can avoid various performance
and/or memory usage problems.
Kind regards,
Alastair.
--
http://alastairs-place.net
_______________________________________________
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