Re: CGImageForProposedRect not scaling image
Re: CGImageForProposedRect not scaling image
- Subject: Re: CGImageForProposedRect not scaling image
- From: Ken Ferry <email@hidden>
- Date: Sun, 9 Jan 2011 08:33:41 -0800
Hi Ken,
On Sat, Jan 8, 2011 at 10:00 PM, Ken Tozier <email@hidden> wrote:
> HI
>
> I'm trying to use NSImage's CGImageForProposedRect method to scale an image
> but it's not doing it. I gat back an image the same size as the original.
Yes - this method doesn't do active work like scaling if it doesn't have to.
It's for the case where you have an NSImage and need it as a CGImage
because that's what some other API takes. From the AppKit release
notes<http://developer.apple.com/library/mac/#releasenotes/Cocoa/AppKit.html>(I
bolded a few sentences):
We've added a method of extracting CGImages from an NSImage, -[NSImage
CGImageForProposedRect:context:hints:]. Why all the parameters? Well, an
NSImage is potentially resolution independent, and may have multiple
representations suited for different contexts. For example, an NSImage may
have a hand drawn 16x16 bitmap as well as a 512x512 bitmap. This allows the
image to display well at large and very small sizes. A CGImage is more like
a single pixel-based representation. Think of
-CGImageForProposedRect:context:hints: as producing a snapshot of how the
NSImage would draw if it was asked to draw in *proposedDestRect in the
passed context.
A snapshot is a good way to think of it, but may lead you to think the
method is less efficient than it really is. *There's nothing guaranteed
about the characteristics of the returned CGImage (e.g., pixel size,
colorspace) except that drawing it in the passed context produces the same
results as drawing the original NSImage in the context. In particular, if an
NSImage is backed by a single NSBitmapImageRep, then it will (probably)
always return the one CGImage that backs the bitmap.* That's a valid
snapshot for the image for any destination rect, because in this case the
CGImage _does_ capture all the drawing the NSImage is capable of.
> I've double-checked all my math and that seems to be right. Here's how it's
> being used anyone see where I'm going wrong?
>
>
> - (BOOL) createThumbnail:(NSString *) inSource
> maxDimension:(int) inMax
> saveTo:(NSString *) inDest
> {
> NSImage *img = [[NSImage alloc] initWithContentsOfFile:
> inSource];
> BOOL result = NO;
>
> //NSLog(@"img: %@", img);
>
> if (img != nil)
> {
> float width = [img size].width,
> height = [img size].height,
> scaledWidth = (width < height) ? inMax * (width
> / height) : inMax ,
> scaledHeight = (width < height) ? inMax : inMax *
> (height / width);
>
> // tried both with and without next two lines, no difference
> in output size
> [img setScalesWhenResized: YES];
> [img setSize: NSMakeSize(scaledWidth, scaledHeight)];
>
> NSRect scaledRect = NSMakeRect(0, 0, scaledWidth,
> scaledHeight);
>
> //NSLog(@"scaledRect: %@", [NSValue valueWithRect:
> scaledRect]);
>
> // Ultimately I want to write the scaled image to a file.
> Should I set a context here? If so, which one and how?
> CGImageRef imgRef = [img CGImageForProposedRect:
> &scaledRect
> context: NULL
> hints: NULL];
>
> if (imgRef != NULL)
> {
> NSURL *destURL = [NSURL fileURLWithPath:
> inDest];
>
> NSDictionary *options = [NSDictionary
> dictionaryWithObjectsAndKeys:
> [NSNumber numberWithInt: 72],
> kCGImagePropertyDPIHeight,
> [NSNumber numberWithInt: 72],
> kCGImagePropertyDPIWidth,
> nil];
>
> CGImageDestinationRef destRef =
> CGImageDestinationCreateWithURL((CFURLRef) destURL,
> kUTTypeJPEG, 1, NULL);
>
>
> if (destRef != NULL)
> {
> // create the image
> CGImageDestinationAddImage(destRef, imgRef,
> (CFDictionaryRef) options);
> CGImageDestinationFinalize(destRef);
> CFRelease(destRef);
>
> if ([[NSFileManager defaultManager]
> fileExistsAtPath: inDest])
> result = YES;
> }
> else
> NSLog(@"Error: Failed to create image
> destination for: %@", inSource);
>
> CFRelease(imgRef);
> }
> else
> NSLog(@"Error: Failed to create image ref for: %@",
> inSource);
>
> [img release];
> }
> else
> NSLog(@"Error: Failed to load image: %@", inSource);
>
> return result;
> }
>
> _______________________________________________
>
> 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
>
_______________________________________________
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