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

Unable To Get UIImage to Match Custom ImagePickerController Preview

我正在构建一个 iOS 应用程序,它具有全屏 ImagePickerController,捕获的结果图像与 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 方法计算不正确,因为生成的图像似乎比需要的 "zoomed in" 多。不太确定计算中有什么问题。

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

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

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

只有 height/scale 需要乘以 4/3,而不是 3/4。我还没有在任何其他设备上测试过这个。只是觉得这可能会帮助任何人 运行 做同样的事情。