iphone - Scaling Images are very slow - Help

2020-03-25 16:06发布

问题:

I am scaling images by myself using the following codes. The code is fine and the images are scaled without problem.

UIImage *originImg = img;
size = newSize;
if (originImg.size.width > originImg.size.height) {
    newSize = CGSizeMake(size.width, originImg.size.height * size.width / originImg.size.width);
} else {
    newSize = CGSizeMake(originImg.size.width * size.height / originImg.size.height, size.height);
}

UIGraphicsBeginImageContext(newSize);

CGContextRef context = CGContextRetain(UIGraphicsGetCurrentContext());
CGContextTranslateCTM(context, 0.0, newSize.height);
CGContextScaleCTM(context, 1.0, -1.0);

CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, newSize.width, newSize.height), originImg.CGImage);

UIImage* scaledImage = [UIGraphicsGetImageFromCurrentImageContext() retain];

UIGraphicsEndImageContext();
CGContextRelease(context);

The only problem is that the scaling process from this code is very slow. Can someone point out how to improve it?

thanks

回答1:

You could try either:

CGContextSetInterpolationQuality(context, kCGInterpolationNone);

or

CGContextSetInterpolationQuality(context, kCGInterpolationLow);

and see if either of those produce any performance differences with an acceptable quality level.