修改:有人建议此帖子是duplicate,但事实并非如此,因为黑条最初不存在,仿射转换无法解决问题问题。
我在iPad Air 2上运行它,目标是iOS 8。
我有一个UIImagePickerController
,其showsCameraControls
属性设置为NO
。在横向启动应用程序然后放大时,会发生这种情况(所有图像都是非裁剪的):
出现一个黑条,可以通过将设备方向更改为纵向(也将显示黑条)然后将其更改回来来消除。
更改为肖像后:
返回风景(+放大):
奇怪的是,在返回横向后,缩放滑块在缩放过程中不再可见。最初从纵向开始时,首先进行缩放,直到其中一个变为横向,其中出现黑条,当回到纵向时,它会停留。
将showsCameraControls
设置为YES
时,不会发生这种情况。
我该怎样摆脱这个问题?
更新:Apple声称已在iOS 9中解决此问题。
答案 0 :(得分:3)
我找到了一种解决我缺乏更好理解的方法来标记错误的方法(就像苹果提供的样本一样)。
我的解决方案是通过向叠加视图添加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