我正在开发一个调整图像大小和合并图像的 iPhone 应用程序。
我想从照片库中选择两张尺寸为 1600x1200 的照片,然后将它们合并为一个图像并将新图像保存回照片库。
但是,我无法为合并的图像获得正确的尺寸。
我拍摄了两个 320x480 帧的图像视图,并将视图的图像设置为我导入的图像。处理图像(缩放、裁剪、旋转)后,我将图像保存到相册。当我检查图像大小时,它显示为 600x800。如何获得1600*1200的原始尺寸?
两周以来我一直被这个问题困扰!
提前致谢。
我正在开发一个调整图像大小和合并图像的 iPhone 应用程序。
我想从照片库中选择两张尺寸为 1600x1200 的照片,然后将它们合并为一个图像并将新图像保存回照片库。
但是,我无法为合并的图像获得正确的尺寸。
我拍摄了两个 320x480 帧的图像视图,并将视图的图像设置为我导入的图像。处理图像(缩放、裁剪、旋转)后,我将图像保存到相册。当我检查图像大小时,它显示为 600x800。如何获得1600*1200的原始尺寸?
两周以来我一直被这个问题困扰!
提前致谢。
Solved as follows.
UIView *bgView = [[UIView alloc] initwithFrame:CGRectMake(0, 0, 1600, 1200)];
UIGraphicsBeginImageContext(tempView.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, self, nil, nil);
Thanks for all your support to solve the issue
UIImageView 的框架与它显示的图像大小无关。如果在 75x75 的 imageView 中显示 1200x1600 像素,则内存中的图像大小仍为 1200x1600。在处理图像的某个地方,您正在重置其大小。
您需要在幕后以编程方式调整图像大小并忽略它们的显示方式。为了获得最高保真度,我建议以全尺寸对图像进行所有处理,然后仅调整最终结果的大小。对于速度和低内存使用,首先调整较小的大小,处理然后根据需要再次调整大小。
我使用Trevor Harmon 的 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 为我节省了数十个试图正确调整大小的工时。