1

我从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?
}
4

1 回答 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;
}
于 2013-08-16T09:46:45.187 回答