iPhone - 放大图像,保持质量
我正在开发一个应用程序,允许用户从相机胶卷中选择照片或拍照。从图片来看,我需要将尺寸增加到1050x670并保持图像质量。我可以放大图像,但当我通过电子邮件发送时,图像显得非常像素化。
在应用程序中,我将把放大的图像发送到服务器进行额外处理。
任何帮助将不胜感激。这是一些代码:
CGSize newSize = CGSizeMake(1050, 670);
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
CGImageRef imageRef = [userImageView.image CGImage];
// Compute the bytes per row of the new image
size_t bytesPerRow = CGImageGetBitsPerPixel(imageRef) / CGImageGetBitsPerComponent(imageRef) * newRect.size.width;
bytesPerRow = 26 * 1050;//(bytesPerRow + 15) & ~15;
CGContextRef bitmap = CGBitmapContextCreate(NULL,
newRect.size.width,
newRect.size.height,
8,
bytesPerRow,
CGImageGetColorSpace(imageRef),
kCGImageAlphaNoneSkipLast);
CGContextSetInterpolationQuality(bitmap, kCGInterpolationHigh);
// Draw into the context; this scales the image
CGContextDrawImage(bitmap, newRect, imageRef);
// Get the resized image from the context and a UIImage
CGImageRef resizedImageRef = CGBitmapContextCreateImage(bitmap);
UIImage *resizedImage = [UIImage imageWithCGImage:resizedImageRef];
// Clean up
CGContextRelease(bitmap);
CGImageRelease(resizedImageRef);
UIImageWriteToSavedPhotosAlbum(resizedImage, nil, nil, nil);
return resizedImage;
I am working on an application that allows a user to pick a photo from their camera roll or take a picture. From the picture, I need to increase the size to be 1050x670 and keep the quality of the image. I am able to increase the image, but the picture comes out very pixelated when I send it through email.
In the app, I will be sending the enlarged image to a server for additional processing.
Any help would be appreciated. Here is some of code:
CGSize newSize = CGSizeMake(1050, 670);
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
CGImageRef imageRef = [userImageView.image CGImage];
// Compute the bytes per row of the new image
size_t bytesPerRow = CGImageGetBitsPerPixel(imageRef) / CGImageGetBitsPerComponent(imageRef) * newRect.size.width;
bytesPerRow = 26 * 1050;//(bytesPerRow + 15) & ~15;
CGContextRef bitmap = CGBitmapContextCreate(NULL,
newRect.size.width,
newRect.size.height,
8,
bytesPerRow,
CGImageGetColorSpace(imageRef),
kCGImageAlphaNoneSkipLast);
CGContextSetInterpolationQuality(bitmap, kCGInterpolationHigh);
// Draw into the context; this scales the image
CGContextDrawImage(bitmap, newRect, imageRef);
// Get the resized image from the context and a UIImage
CGImageRef resizedImageRef = CGBitmapContextCreateImage(bitmap);
UIImage *resizedImage = [UIImage imageWithCGImage:resizedImageRef];
// Clean up
CGContextRelease(bitmap);
CGImageRelease(resizedImageRef);
UIImageWriteToSavedPhotosAlbum(resizedImage, nil, nil, nil);
return resizedImage;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
UIImagePickerController 将返回缩小后的图像。要获取全尺寸图像,您可以使用 ALAssetsLibrary。
http://developer.apple.com/库/ios/#documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html
The UIImagePickerController will return a downscaled image. To obtain the full size image you can use the ALAssetsLibrary.
http://developer.apple.com/library/ios/#documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html