Re: Easiest way to convert an NSImage to a JPEG
Re: Easiest way to convert an NSImage to a JPEG
- Subject: Re: Easiest way to convert an NSImage to a JPEG
- From: Nicko van Someren <email@hidden>
- Date: Thu, 20 May 2004 04:11:00 +0100
On 20 May 2004, at 2:38, John Stiles wrote:
What's the simplest way to go from an NSImage* (or NSImageRep*) to a
JPEG file on my hard disk? i.e. something like:
[myImage createJPEGFile:@"/blat.jpg" withQuality:100.0f]; // this
function is made up
Assuming that you can get your image into a NSBitmapImageRep
*myBitmapImageRep then you can do:
[[myBitmapImageRep representationUsingType: NSJPEGFileType properties:
[NSDictionary dictionaryWithObject: [NSNumber numberWithFloat: 0.5]
forKey: NSImageCompressionFactor]] writeToFile: @"/blat.jpg"
atomically: YES];
Or, if you're not trying to fit it all on one line try:
NSNumber *factor = [NSNumber numberWithFloat: 0.5];
NSDictionary *props = [NSDictionary dictionaryWithObject: factor
forKey: NSImageCompressionFactor];
NSData *jpgData = [myBitmapImageRep representationUsingType:
NSJPEGFileType properties:props];
[jpgData writeToFile: @"/blat.jpg" atomically: YES];
Note that the compression factor is a float between 0.0 and 1.0.
If you're starting off with an image in some non-bitmap format (e.g.
EPS or PDF) you'll have to turn it into a bit mapped image first either
by rendering it or simply by making an NSBitmapImageRep directly from
the output of -TIFFRepresentation on the NSImage as in the line below:
myBitmapImageRep = [NSBitmapImageRep imageRepWithData: [myImage
TIFFRepresentation]];
Since the NSData object for the TIFF representation is an immutable
object there is no extra data copying going on here compared to
rendering the data into a bitmap image of your own devising (the data
just gets retained by the NSBitmapImageRep), although to do have
somewhat less control over the exact rendering.
Cheers,
Nicko
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.