【问题标题】:UIImagePickerController shows black bar when zooming – Is this a bug in iOS?UIImagePickerController 在缩放时显示黑条 - 这是 iOS 中的错误吗?
【发布时间】:2015-05-17 16:01:46
【问题描述】:

编辑有人建议这篇文章是duplicate,但事实并非如此,因为黑条最初不存在,仿射变换不能解决问题。

我在 iPad Air 2 上运行它并针对 iOS 8。 我有一个UIImagePickerController,其showsCameraControls 属性设置为NO。在横向启动应用程序然后放大时,会发生这种情况(所有图像均未裁剪):

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

改成纵向后:

返回横向(+放大):

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

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

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

【问题讨论】:

  • @Ckouta 不,问题似乎有所不同:在我的情况下,该栏仅在缩放后出现。另外,修改cameraViewTransform只会影响视图中非黑色的部分,黑条保持在原处。

标签: ios uiimagepickercontroller screen-orientation uiinterfaceorientation


【解决方案1】:

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

我的解决方案是手动缩放,将UIPinchGestureRecognizer 添加到覆盖视图中。然后控制器必须实现一个缩放回调,这将消除上述现象。

@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 9 上发生。
  • @AkademiksQc 有趣。 Apple 表示他们“相信这个问题已经在 iOS9 中得到解决”。我还没有测试它,但也许我应该更新我的错误报告。虽然我觉得他们会声称这一点很奇怪,因为这个错误是否已被修复是显而易见的。
  • 是的,我实际上是在 9.1
  • 我假设缩放变换已应用于 didFinishPickingMediaWithInfo 委托方法提供的图像,但似乎未应用。我试图用 CG 手动应用它,但我无法让它正常工作......我错过了一些东西。我试图使用 imagePicker.cameraOverlayView.transform.a 应用 CGContextScaleCTM 但这与相机视图上的变换不匹配。你有什么线索吗?谢谢
  • @AkademiksQc 这是前一段时间,所以我不再参与其中,但这是我实际应用转换所做的,也许它有帮助。 pastebin.com/cmeFiUBj cropToSize 方法是定义在一个类中的,不知道有没有必要。 pastebin.com/iDchYmi3
猜你喜欢
  • 1970-01-01
  • 2015-01-06
  • 2017-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多