2017-02-21 69 views
3

我正在研究一個讓用戶從圖庫中選擇照片的應用程序。我遇到的問題非常奇怪,因爲照片的大小(在存儲方面)會在使用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文件還包含示例照片。

任何幫助表示讚賞。

+0

請添加規定的計算你期望什麼等於什麼。 – danh

+0

這個答案解決了這個問題。 http://stackoverflow.com/a/38475923/1120688 – harshitgupta

回答

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?

+0

你的第一個答案是無效的。因爲我自己不想通過壓縮或任何東西來觸摸圖像。我解決了我的問題,使用http://stackoverflow.com/a/38475923/1120688 – harshitgupta

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

    } 
+0

謝謝你的答案,但它不會工作。 – harshitgupta

相關問題