Iphone 如何防止自动旋转AVCapture层时出现“收缩到缩放”手势?
Objective-C,iPhone iOS 6.0 下面的代码很好地支持纵向,但当我尝试横向时,“收缩到缩放”自动将其旋转回笨拙的纵向模式,但在横向视图中Iphone 如何防止自动旋转AVCapture层时出现“收缩到缩放”手势?,iphone,ios,objective-c,Iphone,Ios,Objective C,Objective-C,iPhone iOS 6.0 下面的代码很好地支持纵向,但当我尝试横向时,“收缩到缩放”自动将其旋转回笨拙的纵向模式,但在横向视图中 @implementation AVCapture { CGFloat beginGestureScale; CGFloat effectiveScale; } - (id)initWithView:(UIView *)cameraView { ...... self.previewLa
@implementation AVCapture
{
CGFloat beginGestureScale;
CGFloat effectiveScale;
}
- (id)initWithView:(UIView *)cameraView
{
......
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
[self rotateToInterfaceOrientation:(UIInterfaceOrientation)[UIDevice currentDevice].orientation];
[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
self.previewLayer.frame = cameraView.frame;
[self.previewLayer setPosition:CGPointMake(CGRectGetMidX(cameraView.bounds), CGRectGetMidY(cameraView.bounds))];
[cameraView.layer addSublayer:self.previewLayer];
effectiveScale = 1.0;
......
}
- (void)rotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
CALayer *previewLayer = self.previewLayer;
switch (toInterfaceOrientation)
{
case UIDeviceOrientationLandscapeLeft:
previewLayer.transform = CATransform3DMakeRotation(M_PI+M_PI_2, 0, 0, 1); // 270 degress
break;
case UIDeviceOrientationLandscapeRight:
previewLayer.transform = CATransform3DMakeRotation(M_PI_2, 0, 0, 1); // 270 degress
break;
case UIDeviceOrientationPortraitUpsideDown:
previewLayer.transform = CATransform3DMakeRotation(M_PI, 0, 0, 1); // 270 degress
break;
default:
previewLayer.transform = CATransform3DMakeRotation(0.0, 0, 0, 1); // 270 degress
break;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#pragma mark Pinch to Zoom Gestures
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ( [gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]] )
beginGestureScale = effectiveScale;
return YES;
}
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)recognizer
{
if ([recognizer numberOfTouches] < 2)
return;
BOOL allTouchesAreOnThePreviewLayer = YES;
NSUInteger numTouches = [recognizer numberOfTouches], i;
for (i=0; i<numTouches; ++i)
{
CGPoint location = [recognizer locationOfTouch:i inView:m_CameraView];
CGPoint convertedLocation = [self.previewLayer convertPoint:location fromLayer:self.previewLayer.superlayer];
if (![self.previewLayer containsPoint:convertedLocation])
{
allTouchesAreOnThePreviewLayer = NO;
break;
}
}
if (allTouchesAreOnThePreviewLayer)
{
effectiveScale = beginGestureScale * recognizer.scale;
if (effectiveScale < 1.0)
effectiveScale = 1.0;
CGFloat maxScaleAndCropFactor = [[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] videoMaxScaleAndCropFactor];
if (effectiveScale > maxScaleAndCropFactor)
effectiveScale = maxScaleAndCropFactor;
[CATransaction begin];
[CATransaction setAnimationDuration:.025];
// Scale
[self.previewLayer setAffineTransform:CGAffineTransformMakeScale(effectiveScale, effectiveScale)];
[CATransaction commit];
}
}
我似乎不知道如何保持我的转换。类似的问题在这里得到了回答: 在我看来,由于您正在手动旋转previewLayer,因此需要相应地调整识别器,因为您正在使用缩放覆盖变换。这是因为转换是绝对的,或者至少我是这么认为的 使用我链接的问题中推荐的CGAffineTransformScale,在那里可以传递previewLayer的原始转换,以及所需的比例,怎么样 编辑 以下是解决此问题的文档: 在这里,您可以看到CGAffineTransformMakeScale用于创建转换,而CGAffineTransformScale用于更改转换,我相信这正是您所需要的 编辑2 解决了 发现: ... 更改预览层的方向不会更改捕获图片的方式。对于该AVCaptureConnection,需要使用setVideoOrientation:方法 。。和 所以我替换了这个:
[self rotateToInterfaceOrientation:(UIInterfaceOrientation)[UIDevice currentDevice].orientation];
为此:
[self.previewLayer.connection setVideoOrientation:[UIDevice currentDevice].orientation];
现在,在横向模式+收缩缩放模式下,一切正常: 我尝试过:[self.previewLayer setAffineTransform:cGrafineTransformScaleSelf.previewLayer.affineTransform,effectiveScale,effectiveScale];但是缩放会失去控制。你能描述一下什么是失去控制吗?变换和以前一样吗?当我捏着放大的时候,图像迅速放大到像素级,我似乎不能再缩小了。它不能用了,这不应该发生。有效规模的共同价值是什么?另外,如果我正确阅读了代码,为什么要将recognizer.scale与beginGestureScale相乘呢?原始代码不是我的,所以我正在修复它。有效规模急剧上升。这几乎就像需要对缩放进行限制一样。可能是因为我在使用previewLayer本身吗?
[self rotateToInterfaceOrientation:(UIInterfaceOrientation)[UIDevice currentDevice].orientation];
[self.previewLayer.connection setVideoOrientation:[UIDevice currentDevice].orientation];