2013-08-16 58 views
1

我從UIImagePickerController獲取編輯後的圖像。在視網膜iOS設備上,返回的圖像是640x640,但在非視網膜iOS設備上,返回的圖像僅爲320x320在非視網膜設備上從UIImagePickerController獲取640x640 UIImage

如何從非視網膜設備上的控制器獲得640x640而無需手動升級?因爲我正在上傳,所以無論屏幕如何,我都需要這些尺寸保持不變。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    UIImage *image = info[UIImagePickerControllerEditedImage]; 

    //image.size is 320x320 points on both retina and non-retina devices. 
    //How do I get 640x640 *pixels* for non-retina devices without upscaling? 
} 
+1

你怎麼樣表現出一定的代碼? –

回答

1

我不知道,但可能沒有圖像的大小調整,這是不可能自動獲得視網膜和非視網膜圖像。

所以,您需要通過以下代碼調整您的圖像大小;

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 


    UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage]; 
    img = [self resizeImage:img]; 

    // here you got, img = 640x640 or 320x320 base on you device; 

} 

resizeImage代碼,

- (UIImage*)resizeImage:(UIImage*)image 
{ 
    CGSize newSize = nil; 

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] 
    && [[UIScreen mainScreen] scale] == 2.0) { 
     // Retina 
     newSize = CGSizeMake(640, 640); // Here you need to set size as you want; 
    } else { 
      // Not Retina 
     newSize = CGSizeMake(320, 320); // Here you need to set size as you want; 
    } 

    UIGraphicsBeginImageContext(newSize);// a CGSize that has the size you want 

    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return newImage; 
} 
+0

這簡單地將320x320圖像放大回到640x640,導致質量損失。 – 1actobacillus

+0

這可能不是他們問題的答案,但它確實幫助我解決了相關問題。謝了哥們! – hspain

相關問題