ios - UIImagePickerController 在缩放时显示黑条 – 这是 iOS 中的错误吗?

标签 ios uiimagepickercontroller screen-orientation uiinterfaceorientation

编辑:有人建议这篇文章是 duplicate ,情况并非如此,因为黑条最初并不存在,仿射变换无法解决该问题。

我在 iPad Air 2 上运行此程序,目标是 iOS 8。 我有一个 UIImagePickerController,其 showsCameraControls 属性设置为 NO。当以横向方式启动应用程序然后放大时,会发生以下情况(所有图像均未裁剪):

enter image description here

出现黑条,可以通过将设备方向更改为纵向(这也会显示黑条)然后将其更改回来来消除黑条。

更改为纵向后:

enter image description here

返回横向(+放大):

enter image description here

奇怪的是,返回横向后,缩放 slider 在缩放过程中不再可见。当最初从纵向开始时,首先进行缩放,直到变为横向,此时会出现一个黑条,当返回纵向时,该黑条会保留。

showsCameraControls 设置为 YES 时,不会发生这种情况。 我怎样才能摆脱这个问题?

更新:Apple 声称已在 iOS 9 中修复了此问题。

最佳答案

我找到了一种方法来解决由于缺乏更好的理解而标记为错误的问题(苹果提供的示例也会发生这种情况)。

对我来说,解决方案是通过向覆盖 View 添加 UIPinchGestureRecognizer 来进行手动缩放。然后, Controller 必须实现缩放回调,这将消除上述现象。

@implementation CameraViewController
{
    CGFloat _lastScale; //< the current zoom scale before update
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.imagePicker = [[UIImagePickerController alloc] init];
    self.imagePicker.delegate = self;
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    self.imagePicker.allowsEditing = NO;
    self.imagePicker.showsCameraControls = NO;

    [[NSBundle mainBundle] loadNibNamed:@"CameraOverlay" owner:self options:nil];

    UIPinchGestureRecognizer *pinchRec = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(zoom:)];
    [self.overlayView addGestureRecognizer:pinchRec];

    self.imagePicker.cameraOverlayView = self.overlayView;  
    _lastScale = 1.;
}

- (void)zoom:(UIPinchGestureRecognizer *) sender
{
    // reset scale when pinch has ended so that future scalings are applied cumulatively and the zoom does not jump back (not sure I understand this)
    if([sender state] == UIGestureRecognizerStateEnded)
    {
        _lastScale = 1.0;
        return;
    }

    CGFloat scale = 1.0 - (_lastScale - sender.scale); // sender.scale gives current distance of fingers compared to initial distance. We want a value to scale the current transform with, so diff between previous scale and new scale is what must be used to stretch the current transform


    CGAffineTransform currentTransform = self.imagePicker.cameraViewTransform;
    CGAffineTransform newTransform = CGAffineTransformScale (currentTransform, scale, scale); // stretch current transform by amount given by sender

    newTransform.a = MAX(newTransform.a, 1.); // it should be impossible to make preview smaller than screen (or initial size)
    newTransform.d = MAX(newTransform.d, 1.);

    self.imagePicker.cameraViewTransform = newTransform;
    _lastScale = sender.scale;

}
@end

关于ios - UIImagePickerController 在缩放时显示黑条 – 这是 iOS 中的错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30288950/

相关文章:

ios - 在 View 出现之前是否调用了 viewdidload 方法?

Swift 3 保存从相机拍摄的图像并在数据库中保存路径

android - 方向改变时视频不会调整大小

ios - 如何在 iOS 中使用 takePicture() 以编程方式拍照?

ios - 不使用 UIImagePickerController 获取相机胶卷照片

swift - 设备方向更改时如何更改 UiCollectionView 单元格大小?

html - 在响应式网站中禁用移动用户的横向

ios - Pods-AFNetworking 突然显示错误

ios - 奇怪的 CoreImage 裁剪问题在这里遇到

ios - 使用 Firebase 扁平化数据