views:

96

answers:

2

Here's the code I have but it's crashing ... any ideas?

UIImage *tempImage = [[UIImage alloc] initWithData:imageData];
CGImageRef imgRef = [tempImage CGImage];
 [tempImage release];

 CGFloat width = CGImageGetWidth(imgRef);
 CGFloat height = CGImageGetHeight(imgRef);
 CGRect bounds = CGRectMake(0, 0, width, height);
 CGSize size = bounds.size;

 CGAffineTransform transform = CGAffineTransformMakeScale(4.0, 4.0);

 UIGraphicsBeginImageContext(size);
 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextConcatCTM(context, transform);
 CGContextDrawImage(context, bounds, imgRef);
 UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();

What am I missing here? Basically just trying to scale image up and crop it to be same size as original.

Thanks

+1  A: 

The problem is this line:

CGImageRef imgRef = [tempImage CGImage];

Or more precise, the direct follow-up of this line:

[tempImage release];

You are getting a CF object here, the CGImageRef. Core Foundation object only have the retain/release memory management, but no autoreleased objects. Hence, when you release the UIImage in the second row, the CGImageRef will be deleted as well. And this again means that it's undefined when you try to draw it down there.

I can think of three fixes:

  • use autorelease to delay the release of the image: [tempImage autorelease];
  • move the release to the very bottom of your method
  • retain and release the image using CFRetain and CFRelease.
Max Seelemann
A: 

Have a look at this blog entry. It covers many aspects in resizing, cropping and making round corners.

epatel