UIImagePickerController返回比原始

时间:2017-02-21 21:05:00

标签: ios objective-c size jpeg uiimagepickercontroller

我正在开发一款让用户从图库中选择照片的应用。我面临的问题非常奇怪,因为当使用UIImagePickerController拾取照片时,照片的大小(就存储而言)会发生变化。

在我的情况下,我通过空投拍了一张照片。图像大小为8.7MB。但是当我通过UIImagePickerController选择相同的图像时,它会返回~13MB的图像。

注意:图像的分辨率保持不变([3024,4032])。

我创建了一个非常简单的应用来测试这个东西。以下是示例代码:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@end

ViewController.m

#import "ViewController.h"

@implementation ViewController

bool flag = true;

- (void)viewDidAppear:(BOOL)animated {
    if (flag) {
        flag = false;
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        [self presentViewController:picker
                           animated:YES
                         completion:NULL];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
    NSData *temp  = UIImageJPEGRepresentation(chosenImage, 1);
    NSLog(@"image: %lu", (unsigned long)temp.length);
    NSLog(@"image: [%lu, %lu]", (unsigned long)chosenImage.size.width, (unsigned long)chosenImage.size.height);
    [picker dismissViewControllerAnimated:YES completion:NULL];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES
                               completion:NULL];
}

@end

this is the link to the app,以防您想亲自测试。

zip文件还包含示例照片。

感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

这是因为您从拾取器(作为UIImage)获取未压缩的图像。然后使用UIImageJPEGRepresentation()将其转换为jpeg,并将1.0作为最高质量(压缩率最低)的压缩质量传递。如果您传递较低的数字(如0.5),则结果数据会更小。请参阅:https://developer.apple.com/reference/uikit/1624115-uiimagejpegrepresentation?language=objc

<强>更新

如果您想要原始文件数据,可以使用UIImagePickerControllerReferenceURL,请参阅此处:How do I get the data from UIImagePickerControllerReferenceURL?

答案 1 :(得分:0)

请参阅此代码

    -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
    {
        UIImage* chosenImage = [info valueForKey:@"UIImagePickerControllerOriginalImage"];

        NSLog(@"Image Size Width %f  Height %f",chosenImage.size.width,chosenImage.size.height);

        UIGraphicsBeginImageContext(CGSizeMake(320, 480));
        [originalImage drawInRect:CGRectMake(0,0,320,480)];
        UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        NSLog(@"image Size h %f  Width %f",[image size].height, [image size].width);

    }