How to incrementally compress jpeg?
How to incrementally compress jpeg?
- Subject: How to incrementally compress jpeg?
- From: Phi Le <email@hidden>
- Date: Fri, 20 Nov 2009 13:47:35 -0800
I posted this question at
http://stackoverflow.com/questions/1773360/how-to-compress-jpeg-image-with-cocoa
.
Copy and paste:
I have an jpeg image and I want to be able to incrementally compress
it using Cocoa/Core Image/Core Graphics. For example, I have A.jpg
(3MB), I compress A and get B.jpg (1MB), compress B and get C.jpg
(400KB), and so on till the image can't be compressed anymore.
I am trying to use NSBitmapImageRep representationUsingType:properties
without much success. I use an image rep created from compressed
image data to try and compress further, but this doesn't work well, if
I use a compression factor that is the same factor used to create the
initial compressed data.
Here is the code that I have been wrangling with
<code>
NSString * fileName = @"largeImage.jpg";
NSLog(@"== Initializing ==\n");
NSData * imageData = [NSData dataWithContentsOfFile:fileName];
NSLog(@"imageData length: %d\n", [imageData length]);
NSImage * image = [[NSImage alloc] initWithData:imageData];
NSBitmapImageRep * bitmapRep = [[image representations] objectAtIndex:0];
NSData * data = [bitmapRep representationUsingType:NSJPEGFileType
properties:nil];
NSLog(@"data length: %d\n", [data length]);
NSDictionary * dict = [NSDictionary dictionaryWithObject: [NSNumber
numberWithFloat:0.5] forKey:NSImageCompressionFactor];
NSData * data5 = [bitmapRep representationUsingType:NSJPEGFileType
properties:dict];
NSLog(@"data5 length: %d\n", [data5 length]);
NSLog(@"== End ==\n\n");
NSLog(@"== Image from Data5 ==\n");
NSImage * imageFromData5 = [[NSImage alloc] initWithData:data5];
NSBitmapImageRep * bitmapRepFromData5 = [[imageFromData5
representations] objectAtIndex:0];
NSData * dataFromImageFromData5 = [bitmapRepFromData5
representationUsingType:NSJPEGFileType properties:nil];
NSLog(@"dataFromImageFromData5 length: %d\n", [dataFromImageFromData5 length]);
dict = [NSDictionary dictionaryWithObject: [NSNumber
numberWithFloat:0.5] forKey:NSImageCompressionFactor];
NSData * dataFiveFromImageWithData5 = [bitmapRepFromData5
representationUsingType:NSJPEGFileType properties:dict];
NSLog(@"dataFiveFromImageWithData5 length: %d\n",
[dataFiveFromImageWithData5 length]);
NSLog(@"== End ==\n\n");
</code>
This is my result
<output>
> == Initializing ==
> imageData length: 1882694
> data length: 1888365
> data5 length: 1102461
> == End ==
> == Image from Data5 ==
> dataFromImageFromData5 length: 1646137
> dataFiveFromImageWithData5 length: 1102115
> == End ==
Try a different image
> == Initializing ==
> imageData length: 3620257
> data length: 2889458
> data5 length: 1404750
> == End ==
> == Image from Data5 ==
> dataFromImageFromData5 length: 2007951
> dataFiveFromImageWithData5 length: 1405398
> == End ==
</output>
_______________________________________________
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