• 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: Fastest way to resize an image?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Fastest way to resize an image?


  • Subject: Re: Fastest way to resize an image?
  • From: Heinrich Giesen <email@hidden>
  • Date: Sat, 22 Nov 2008 17:59:18 +0100

Hi,

On 20.11.2008, at 09:05, Jean-Nicolas Jolivet wrote:

I have a bunch of images to resize.


anyway, right now I am loading an NSBitmapImageRep with the
imageRepFromFile:  method... then creating a new, empty NSImage based
on the image size that I want to resize to (using initWithSize:) and I
draw the BitmapImageRep on this image (while the focus is locked on
the NSImage)...


I understand that you are not happy with this way, I call this way the old way,
which is now outdated but not deprecated, because since Tiger you can draw directly
into an NSBitmapImageRep. (The above way first creates --lockfocus does it--
and then draws into an NSCachedImageRep, from which a new NSBitmapImageRep
can be made via TIFFRepresentation.)


I usually use the following code (or a similar one) which respects the size
(resolution) of the sourceImage reliably!
(Searching in this forum for graphicsContextWithBitmapImageRep: will give more hints.)



// first load the sourceImage
NSBitmapImageRep *sourceRep = [[NSBitmapImageRep alloc] initWithData: ..dataFromFile ..];


NSBitmapImageRep *newRep =
[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:[sourceRep pixelsWide] // or a new value
pixelsHigh:[sourceRep pixelsHigh]
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES // must have alpha!
isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bytesPerRow:0
bitsPerPixel:0 ];
[NSGraphicsContext saveGraphicsState];
NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithBitmapImageRep:newRep];
[NSGraphicsContext setCurrentContext:context];


// do not use drawAtPoint: !! it does not respect resolution due to a bug
[self drawInRect:NSMakeRect( 0, 0, [sourceRep pixelsWide], [sourceRep pixelsHigh])];


[NSGraphicsContext restoreGraphicsState];
[newRep setSize:[sourceRep size]]; // this sets the resolution of the source
[newRep autorelease]; // if needed



Instead of [sourceRep pixelsWide] you should set the new number of pixels. This value
must be the same in pixelsWide: and in the drawRect.


You see: NSImage is not used because we don't need it. If more flexible drawOperations
are waanted, I use a new temporary NSImage and remove it as soon as possible.


My tests showed a time speedup of at least a factor of 5.

BTW: the connection between size, pixelnumbers and resolution is:

size.width = 72.0*pixelsWide / resolutionX
size.height = 72.0*pixelsHigh / resolutionY
(size has the dimension of a length and the unit 1/72 inch;
resolution has the dimension "dots per length" and the
unit "dots per inch"; pixelsWide, pixelsHigh are dimensionless numbers.)



good luck Heinrich


-- Heinrich Giesen email@hidden


_______________________________________________

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


  • Follow-Ups:
    • Re: Fastest way to resize an image?
      • From: Jean-Nicolas Jolivet <email@hidden>
    • Re: Fastest way to resize an image?
      • From: Jean-Nicolas Jolivet <email@hidden>
  • Prev by Date: Re: Responding to view controller memory warnings (was Re: Outlets / IBOutlet declarations)
  • Next by Date: Re: Responding to view controller memory warnings (was Re: Outlets / IBOutlet declarations)
  • Previous by thread: Re: Fastest way to resize an image?
  • Next by thread: Re: Fastest way to resize an image?
  • Index(es):
    • Date
    • Thread