NSBitMapRepresentation --> JPEG compression?
NSBitMapRepresentation --> JPEG compression?
- Subject: NSBitMapRepresentation --> JPEG compression?
- From: Chris Tracewell <email@hidden>
- Date: Mon, 29 Jun 2009 16:12:42 -0700
--- Using GC ---
I am saving images to file from an NSImage dropped into an NSImageView
using JPEG as the file format via the code below.
-(IBAction)saveImages:(id)sender
{
TKProduct *theProduct = [[myProductArrayController selectedObjects]
objectAtIndex:0];
NSBitmapImageRep *theBitMapImageRep = [NSBitmapImageRep
imageRepWithData:[[theProduct myImage] TIFFRepresentation]];
NSDictionary *thePropertyDictionary = [NSDictionary
dictionaryWithObjectsAndKeys:[theProduct
myImageCompression],NSImageCompressionFactor,[NSNumber
numberWithInteger:0],NSImageProgressive,nil];
NSString *theFileName = [NSString stringWithFormat:@"%@-%@.jpg",
[theProduct myID],[theProduct myImageCompression]];
NSData *theData = [theBitMapImageRep representationUsingType:
NSJPEGFileType properties: thePropertyDictionary];
[theData writeToFile:[@"/volumes/MyHardDrive/Users/myname/Desktop/
XCode-Image-Tests/" stringByAppendingString:theFileName]
atomically:YES];
}
Ideally I am wanting to achieve quality and file size comparable with
what I get out of Photoshop's File->Save For Web command. I am
satisfied with the comparative quality of matching settings I get -
that is an image saved from PS at JPEG quality 50 and non progressive
is close enough (although not quite as good) as what I get from the
same image run through the method above using NSImageCompressionFactor
of 0.5. The issue is that when using writeToFile my images have an
extra 3 to 10 KB of size whether the image is really small or really
big - ie 5KB or 300KB. I am suspecting that my XCode generated images
are bigger for one of two reasons...
1) Adobe's JPEG compression is just plain better and unless I develop
my own JPEG compression algorithm I am hosed.
2) My Objective-C created images contain extra file meta data /
headers that are inflating the size a bit
I mention scenario 2 because a few versions back, before "Save For
Web" was in PS, you could uncheck an option for "Include Extra File
Headers" (or something like that) when saving to JPEG and your files
would always shrink by a few KB. The file size is important because
the images will load on a a busy web site up to a hundred per page
load and so 100 4KB image thumbnails loads a lot quicker than 100 8KB
thumbnails. I have always used ImageReady and AppleScript to process
these files in the past but want to skip that and do it solely in my
App.
Does anyone have any suggestions as to how to squeeze a little more
file size reduction out of these JPEG's without having to dive into
JPEG compression algorithms (probably over my head, but willing to
look if you want to point in a direction :-) ? I guess I am hoping
there is something I have missed in NSData and writeToFile that allows
me to not include custom OS X meta data / headers / Icons.
.... I should also mention that if you have any pointers for PNG
optimization, tutorials, tech docs, I would love the links as PNG is a
format I would like to use down the road as well and the docs and
Google didn't provide anything about how to actually produce a reduced
color pallete (I know its probably right in front of me, please feel
free to point out the obvious :-)
Thank you.
_______________________________________________
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