如何从UIImagePickerController设置保存图像的分辨率

Bar*_*K88 0 iphone scale uiimageview uiimagepickercontroller

我正在使用UIImagePickerController将照片中的图片设置为UIImageView.然而,在UIImageView中,图像会自动缩放,因为我在IB中提到了"缩放以适应".我想以80x80像素的分辨率保存我选择的图像,这样我就不必进行缩放.(由于这个扩展问题,我的应用程序变得非常缓慢.)

这是我的代码中的一个snippit:

-(void)imagePickerController:(UIImagePickerController *)picker
      didFinishPickingImage : (UIImage *)image
                 editingInfo:(NSDictionary *)editingInfo
{
    [picker dismissModalViewControllerAnimated:YES];
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
        photoView.image = image;


        NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];       
        NSString *pngFilePath = [NSString stringWithFormat:@"%@/foto1.png",docDir];
        NSData *data = [NSData dataWithData:UIImagePNGRepresentation(image)];

        [data writeToFile:pngFilePath atomically:YES];
}
Run Code Online (Sandbox Code Playgroud)

非常感谢帮助!

Sab*_*bin 5

我在这里找到了这个代码:Link

//  ==============================================================
//  resizedImage
//  ==============================================================
// Return a scaled down copy of the image.  

UIImage* resizedImage(UIImage *inImage, CGRect thumbRect)
{
    CGImageRef          imageRef = [inImage CGImage];
    CGImageAlphaInfo    alphaInfo = CGImageGetAlphaInfo(imageRef);

    // There's a wierdness with kCGImageAlphaNone and CGBitmapContextCreate
    // see Supported Pixel Formats in the Quartz 2D Programming Guide
    // Creating a Bitmap Graphics Context section
    // only RGB 8 bit images with alpha of kCGImageAlphaNoneSkipFirst, kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedFirst,
    // and kCGImageAlphaPremultipliedLast, with a few other oddball image kinds are supported
    // The images on input here are likely to be png or jpeg files
    if (alphaInfo == kCGImageAlphaNone)
        alphaInfo = kCGImageAlphaNoneSkipLast;

    // Build a bitmap context that's the size of the thumbRect
    CGContextRef bitmap = CGBitmapContextCreate(
                NULL,
                thumbRect.size.width,       // width
                thumbRect.size.height,      // height
                CGImageGetBitsPerComponent(imageRef),   // really needs to always be 8
                4 * thumbRect.size.width,   // rowbytes
                CGImageGetColorSpace(imageRef),
                alphaInfo
        );

    // Draw into the context, this scales the image
    CGContextDrawImage(bitmap, thumbRect, imageRef);

    // Get an image from the context and a UIImage
    CGImageRef  ref = CGBitmapContextCreateImage(bitmap);
    UIImage*    result = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);   // ok if NULL
    CGImageRelease(ref);

    return result;
}
Run Code Online (Sandbox Code Playgroud)