On May 31, 2010, at 5:38 AM, Simon Raisin wrote:

I am trying to reduce the size of an NSImage by 50% and then save the
reduced image out to disk. The following code does not reduce the image.
Should I be going about this a different way?


NSImage* inputImage = [[NSImage alloc] initWithContentsOfFile: [fileNames objectAtIndex:0]];
NSSize halfSize = [inputImage size];

halfSize.height = (int)(halfSize.height / 2);
halfSize.width  = (int)(halfSize.width  / 2);

[inputImage setScalesWhenResized:YES];
[inputImage setSize:halfSize];

NSBitmapImageRep* imageRepx = [NSBitmapImageRep imageRepWithData: [inputImage TIFFRepresentation]]; NSDictionary *imagePropsx = [NSDictionary dictionaryWithObject: [NSNumber numberWithFloat:0.9] forKey:NSImageCompressionFactor]; NSData* outputImageDatax = [imageRepx representationUsingType:NSJPEGFileType properties:imagePropsx];

[outputImageDatax writeToFile:@"resizedimage.jpg" atomically:YES];

As Jens mentioned, -[NSImage setSize:] doesn't actually resize the image; -[NSImage size] works with -[NSImageRep size] to determine the DPI for the image, but you haven't changed the number of pixels in the underlying NS*ImageRep.

If you want to resize the image so that there are width x height pixels, you'll need to create a new image and draw the original into it.

NSImage* inputImage = [NSImage alloc] initWithContentsOfFile: [fileNames objectAtIndex:0]];
NSRect   outputBounds = {NSZeroPoint, [inputImage size]};

outputBounds.size.width = floor(NSWidth(outputBounds) / 2);
outputBounds.size.height = floor(NSHeight(outputBounds) / 2);

NSImage* outputImage = [[NSImage alloc] initWithSize: outputBounds.size];
NSBitmapImageRep* outputBitmap = [[NSBitmapImageRep alloc]
                        initWithBitmapDataPlanes:NULL
                                      pixelsWide:NSWidth(outputBounds)
                                      pixelsHigh:NSHeight(outputBounds)
                                   bitsPerSample:8
                                 samplesPerPixel:4
                                        hasAlpha:NO
                                        isPlanar:NO
                                  colorSpaceName:NSCalibratedRGBColorSpace
                                     bytesPerRow:0
                                    bitsPerPixel:0];

[outputImage addRepresentation:outputBitmap];
[outputImage lockFocusOnRepresentation:outputBitmap];
[inputImage drawInRect: outputBounds fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
[outputImage unlockFocus];

NSDictionary* imageProps = [NSDictionary dictionaryWithObject: [NSNumber numberWithFloat:0.9] forKey:NSImageCompressionFactor]; NSData* outputImageData = [outputBitmap representationUsingType:NSJPEGFileType properties:imageProps];

[outputImageData writeToFile:@"resizedimage.jpg" atomically:YES];

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to