views:

37

answers:

1

I am attempting to take an image captured from the rear camera, apply a scaling transformation to it and then output a new image with the same size of the original but scaled up.

What I have below works ... BUT it is slow (especially the code in between the UIGraphicsBeginImageContext(size); and UIGraphicsEndImageContext();)

Is there a better way of doing this? In particular, a way that will produce the same result but faster?

UIImage *tempImage = [[UIImage alloc] initWithData:imageData];

CGImageRef img = [tempImage CGImage];
CGFloat width = CGImageGetWidth(img);
CGFloat height = CGImageGetHeight(img);
CGRect bounds = CGRectMake(0, 0, width, height);
CGSize size = bounds.size;

CGFloat z = 5.0; 
CGAffineTransform xfrm = CGAffineTransformMakeScale(z, z);

UIGraphicsBeginImageContext(size);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextConcatCTM(currentContext, xfrm);
CGContextDrawImage(currentContext, bounds, img);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

Thanks - wg

A: 

The answer is simple: Crop and then scale

The size of a UIImage can be considerable especially when captured via the camera in "photo" quality resulting in poor performance when scaling it up. So, best practice is to first crop the photo to the needed size AND then scale as needed.

wgpubs