在非视网膜设备上从UIImagePickerController获取640x640 UIImage

时间:2013-08-16 09:38:16

标签: iphone ios objective-c uiimage uiimagepickercontroller

我从UIImagePickerController获得了一张经过编辑的图片。在视网膜iOS设备上,返回的图像为 640x640 ,但在非视网膜iOS设备上,返回的图像仅 320x320

如何在没有手动升级的情况下从非视网膜设备上的控制器获取 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 个答案:

答案 0 :(得分: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;
}