iPhone - 放大图片,保持品质

时间:2010-06-28 22:36:14

标签: iphone objective-c uiimage

我正在开发一个应用程序,允许用户从相机胶卷中挑选照片或拍照。从图片中,我需要将大小增加到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;

1 个答案:

答案 0 :(得分:0)

UIImagePickerController将返回缩小的图像。要获得完整大小的图像,您可以使用ALAssetsLibrary。

http://developer.apple.com/library/ios/#documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html