UIImagePickerController 在缩放时显示黑条 – 这是 iOS 中的错误吗? [英] UIImagePickerController shows black bar when zooming – Is this a bug in iOS?
问题描述
编辑:有人建议此帖子是重复,情况并非如此,因为黑条最初不存在,仿射变换不能解决问题.
Edit: It has been suggested this post is a duplicate, which is not the case as the black bars are not initially present an an affine transformation does not remedy the problem.
我在 iPad Air 2 上运行它并针对 iOS 8.我有一个 UIImagePickerController
,它的 showsCameraControls
属性设置为 NO
.在横向启动应用程序然后放大时,会发生这种情况(所有图像均未裁剪):
I run this on an iPad Air 2 and target iOS 8.
I have a UIImagePickerController
whose showsCameraControls
property is set to NO
. When starting the app in landscape and then zooming in, this is what happens (all images are non-cropped):
出现黑条,可以通过将设备方向更改为纵向(也会显示黑条)然后将其改回来来消除.
A black bar appears and can be gotten rid of by changing device orientation to portrait (which will also show the black bar) and then changing it back.
更改为纵向后:
返回横向(+ 放大):
Back to landscape (+ zooming in):
奇怪的是,返回横向后,缩放滑块在缩放过程中不再可见.最初从纵向开始时,首先进行缩放,直到变为横向,此时会出现一个黑条,返回纵向时会保持不变.
Strangely, after returning to landscape, the zoom slider is not visible anymore during zooming. When starting from portrait initially, zooming first works until one changes to landscape, where a black bar appears, which stays when going back to portrait.
当将 showsCameraControls
设置为 YES
时,这些都不会发生.我怎样才能摆脱这个问题?
None of this happens when setting showsCameraControls
to YES
.
How can I get rid of this issue?
更新:Apple 声称已在 iOS 9 中修复此问题.
UPDATE: Apple claims to have fixed this in iOS 9.
推荐答案
我找到了一种方法来解决我缺乏更好理解的问题,我会标记错误(苹果提供的示例也会发生这种情况).
I have found one way to work around what I for lack of better understanding would label a bug (as it happens with apple-provided samples as well).
我的解决方案是手动缩放,方法是将 UIPinchGestureRecognizer
添加到叠加视图.然后控制器必须实现缩放回调,以消除上述现象.
The solution for me is to do manual zooming, by adding a UIPinchGestureRecognizer
to the overlay view. The Controller must then implement a zooming callback which will get rid of the phenomenon described above.
@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
这篇关于UIImagePickerController 在缩放时显示黑条 – 这是 iOS 中的错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!