开发者

What is the fastest way to generate a new UIImage from transforms applied to an existing UIImage?

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 UIGraphicsEnd开发者_Python百科ImageContext();)

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜