Re: Resizing an image
Re: Resizing an image
- Subject: Re: Resizing an image
- From: "Glenn Bloom" <email@hidden>
- Date: Sat, 13 Dec 2008 01:41:07 -0500
So it looks like I now have a better solution for copying an image file and
reducing its size both in dimensions and in bytes, thanks to a couple of
recommendations I received. Of course, other recommendations made clear
that I need to learn more about alternative approaches:
- some experimentation today with CoreImage only went so far, and I still
need to understand more
- And I still need to understand in particular whether or not a CoreImage
approach would require use of filters (this will matter because I am under
the impression that the iPhone does not support CoreImage filters and I need
to think about that too)
And I also just tripped across the following thread which suggests there may
be image quality issues to take into account eventually, with one approach
or another:
http://www.cocoabuilder.com/archive/message/cocoa/2008/1/4/195871
For now, I have the approach demonstrated in my test below (employing
NSImage to accessing a JPEG, and then writing a copy as a smaller file), and
I just need to learn more and understand if there are compelling reasons to
use alternative techniques:
- (IBAction) showImage:(id)sender{
NSInteger widthInput, heightInput;
NSString *inputFileFullPath;
NSString *outputFileFullPath;
NSImage* inputImage;
NSImage* outputImage;
NSImage* outputImageFromFile;
inputFileFullPath =
@"/Users/User/ResourcesTestData/testDataInput/testImage2MB.jpg";
outputFileFullPath =
@"/Users/User/ResourcesTestData/testDataOutput/testImageSmaller.jpg";
// Determine the dimensions of the input file -------------
inputImage = [[NSImage alloc] initWithContentsOfFile:inputFileFullPath];
NSBitmapImageRep *imageRep00;
imageRep00 = [[NSBitmapImageRep alloc] initWithData:[inputImage
TIFFRepresentation]];
widthInput = [imageRep00 pixelsWide];
heightInput = [imageRep00 pixelsHigh];
NSLog(@"inputImage (w,h): (%i, %i)", widthInput, heightInput);
// Create a smaller NSImage object ----------------------------
NSSize newSize = NSMakeSize (widthInput*.05, heightInput*.05);
outputImage = [[NSImage alloc] initWithSize:newSize];
[outputImage lockFocus];
[[NSGraphicsContext currentContext] setImageInterpolation:
NSImageInterpolationHigh];
[inputImage drawInRect:NSMakeRect(0, 0, newSize.width, newSize.height)
fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
[outputImage unlockFocus];
// Write the smaller NSImage object to a file, and compress it
NSBitmapImageRep *imageRep01;
imageRep01 = [[NSBitmapImageRep alloc] initWithData:[outputImage
TIFFRepresentation]];
NSData *imageData;
double compression = 0.0;
NSDictionary *props = [NSDictionary
dictionaryWithObject:
[NSNumber numberWithFloat:compression]
forKey:NSImageCompressionFactor];
imageData = [imageRep01 representationUsingType:NSJPEGFileType
properties:props];
[imageData writeToFile:outputFileFullPath atomically:YES];
// Retrieve and display the file written
outputImageFromFile = [[NSImage alloc]
initWithContentsOfFile:outputFileFullPath];
[imageView setImage:outputImageFromFile];
// Determine the dimensions of the output file -------------
NSBitmapImageRep *imageRep02;
imageRep02 = [[NSBitmapImageRep alloc] initWithData:[outputImageFromFile
TIFFRepresentation]];
widthInput = [imageRep02 pixelsWide];
heightInput = [imageRep02 pixelsHigh];
NSLog(@"inputImage (w,h): (%i, %i)", widthInput, heightInput);
[imageRep00 release];
[imageRep01 release];
[imageRep02 release];
[inputImage release];
[outputImage release];
[outputImageFromFile release];
}
>
>
>
>
_______________________________________________
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