UIImageView 的框架与它显示的图像大小无关。如果在 75x75 的 imageView 中显示 1200x1600 像素,则内存中的图像大小仍为 1200x1600。在您处理图像的某个地方,您正在重置其大小。
您需要在幕后以编程方式调整图像大小并忽略它们的显示方式。为了获得最高保真度,我建议以全尺寸对图像进行所有处理,然后仅调整最终结果的大小。对于速度和低内存使用,首先调整较小的大小,处理然后根据需要再次调整大小。
我使用Trevor Harmon's UIImage+Resize 来调整图像大小。
他的核心方法是这样的:
- (UIImage *)resizedImage:(CGSize)newSize
transform:(CGAffineTransform)transform
drawTransposed:(BOOL)transpose
interpolationQuality:(CGInterpolationQuality)quality
{
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
CGRect transposedRect = CGRectMake(0, 0, newRect.size.height, newRect.size.width);
CGImageRef imageRef = self.CGImage;
// Build a context that's the same dimensions as the new size
CGContextRef bitmap = CGBitmapContextCreate(NULL,
newRect.size.width,
newRect.size.height,
CGImageGetBitsPerComponent(imageRef),
0,
CGImageGetColorSpace(imageRef),
CGImageGetBitmapInfo(imageRef));
// Rotate and/or flip the image if required by its orientation
CGContextConcatCTM(bitmap, transform);
// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(bitmap, quality);
// Draw into the context; this scales the image
CGContextDrawImage(bitmap, transpose ? transposedRect : newRect, imageRef);
// Get the resized image from the context and a UIImage
CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
// Clean up
CGContextRelease(bitmap);
CGImageRelease(newImageRef);
return newImage;
}
Harmon 为我节省了数十个尝试正确调整大小的工时。