UIImagePickerController返回比原始图像更大的图像 [英] UIImagePickerController returns bigger Image than the originial

查看:87
本文介绍了UIImagePickerController返回比原始图像更大的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on an app that let the users choose photo from the gallery. The problem I am facing is very weird as the size of the photo(in terms of storage) changes when its picked up using UIImagePickerController.

以我为例,我通过空投得到了一张照片.图像大小为8.7MB.但是当我通过UIImagePickerController选择相同的图像时,它将返回约13MB的图像.

In my case, I got a picture via air-drop. The image size is 8.7MB. but when I pick the same image via UIImagePickerController, it returns me the image of ~13MB.

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

Note: the resolution of the image remains the same([3024, 4032]).

我创建了一个非常简单的应用程序来测试它.这是示例代码:

I created a very simple app to test the thing. Here is sample code:

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, in case you want to test it for yourself.

该zip文件还包含示例照片.

The zip file also contains the sample photos.

感谢您的帮助.

推荐答案

请参考此代码

    -(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);

    }

这篇关于UIImagePickerController返回比原始图像更大的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆