无法获取 UIImage 以匹配自定义 ImagePickerController 预览

Posted

技术标签:

【中文标题】无法获取 UIImage 以匹配自定义 ImagePickerController 预览【英文标题】:Unable To Get UIImage to Match Custom ImagePickerController Preview 【发布时间】:2015-04-26 17:38:48 【问题描述】:

我正在构建一个具有全屏 ImagePickerController 的 ios 应用程序,捕获的结果图像与 ImagePickerController 视图中显示的图像相同。这是我当前的相关代码:

创建和转换 ImagePickerController:

    self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;


CGSize screenSize = [[UIScreen mainScreen] bounds].size;

// set the aspect ratio of the camera
float heightRatio = 4.0f / 3.0f;
// calculate the height of the camera based on the screen width
float cameraHeight = floorf(screenSize.width * heightRatio);
// calculate the ratio that the camera height needs to be scaled by
float scale = ceilf((screenSize.height / cameraHeight) * 10.0) / 10.0;

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;


    self.imagePicker.showsCameraControls = NO;
    [self.imagePicker setCameraOverlayView:cameraView];

    // move the controller to the center of the screen
    self.imagePicker.cameraViewTransform = CGAffineTransformMakeTranslation(0, (screenSize.height - cameraHeight) / 2.0);
    // concatenate the scale transform
    self.imagePicker.cameraViewTransform = CGAffineTransformScale(self.imagePicker.cameraViewTransform, scale, scale);

捕获图像后,这是我用来重绘捕获的图像以匹配预览的代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
 self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
 self.image = [self croppedImage:self.image];

- (UIImage *) croppedImage: (UIImage *)image

CGSize screenSize = [[UIScreen mainScreen] bounds].size;    
// set the aspect ratio of the camera
float heightRatio = 4.0f / 3.0f;
// calculate the height of the camera based on the screen width
float cameraHeight = floorf(screenSize.width * heightRatio);
// calculate the ratio that the camera height needs to be scaled by
float scale = ceilf((screenSize.height / cameraHeight) * 10.0) / 10.0;


CGSize originalImageSize = [image size];
CGSize newImageSize = CGSizeMake(floorf(originalImageSize.width / scale)* 3/4, floorf(originalImageSize.height / scale)* 3/4);

CGRect newImageRect = CGRectMake((originalImageSize.width - newImageSize.width)/2.0, (originalImageSize.height - newImageSize.height)/2.0, newImageSize.width, newImageSize.height);

return [image croppedImage:newImageRect];

所以我的问题是我的 CroppedImage 方法计算不正确,因为生成的图像似乎比需要的更“放大”。不太确定计算中有什么问题。

注意 - 此应用旨在在所有 iPhone 上正确缩放 - 仅限纵向模式。我目前正在 iPhone 6 上进行测试。

【问题讨论】:

【参考方案1】:

如果这对任何人都有帮助 - 在我的设备上,我可以通过切换来修复它

       CGSize newImageSize = CGSizeMake(floorf(originalImageSize.width / scale)* 3/4, floorf(originalImageSize.height / scale)* 3/4);

       CGSize newImageSize = CGSizeMake(floorf(originalImageSize.width / scale), floorf(originalImageSize.height / scale)* 4/3);

只有需要乘以 4/3 而不是 3/4 的高度/比例。我还没有在任何其他设备上测试过这个。只是想这可能会帮助任何遇到同样事情的人。

【讨论】:

以上是关于无法获取 UIImage 以匹配自定义 ImagePickerController 预览的主要内容,如果未能解决你的问题,请参考以下文章

使用 UIButton 自定义 UITableViewCell:无法更改 UIImage

AFNetworking 后无法获取 UIImage 大小高度

Swift:处理 UIImage 数据以用于 Firebase 自定义 TFLite 模型

居中包含 UIImage 和 UILabel 的自定义 TitleView

无法从 URL 获取图像以在 UIImage 中快速更新

自定义 UIBarButton 项图像