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;
}
哈蒙救了我幾十個工時試圖讓尺寸調整正確。
問題可能是太含糊回答 - 嘗試是模式詳細和具體的 – 2010-03-08 12:54:07
感謝答覆, 我使用尺寸1200 * 1600的兩個圖像。爲了iPhone的兼容性,我正在採取320 * 480的圖像視圖來顯示兩個圖像...之後,即時通訊從這兩幅圖像中繪製出一個新的圖像,其尺寸爲320 * 480,但我需要它的原始尺寸爲1200 * 1600尺寸..我怎樣才能得到。目前正在調整圖像的大小爲1200 * 1600,但它給我模糊的圖像。 – Madhu 2010-03-08 13:02:16