• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Reduce the size of an NSImage
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reduce the size of an NSImage


  • Subject: Re: Reduce the size of an NSImage
  • From: Steve Christensen <email@hidden>
  • Date: Tue, 01 Jun 2010 15:07:30 -0700

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 (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


References: 
 >Reduce the size of an NSImage (From: Simon Raisin <email@hidden>)

  • Prev by Date: Re: -[NSDocument fileURL] is observable (KVO). Documented?
  • Next by Date: Re: -[NSDocument fileURL] is observable (KVO). Documented?
  • Previous by thread: Re: Reduce the size of an NSImage
  • Next by thread: nibs don't know about protocols?
  • Index(es):
    • Date
    • Thread