CGImageForProposedRect not scaling image
CGImageForProposedRect not scaling image
- Subject: CGImageForProposedRect not scaling image
- From: Ken Tozier <email@hidden>
- Date: Sun, 9 Jan 2011 01:00:35 -0500
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. 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