1

Objective-C,iPhone iOS 6.0

下面的代码很好地支持纵向方向,但是当我尝试横向时,捏缩放自动将其旋转回尴尬的纵向模式(但在横向视图中)。

@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];
    }

}

我似乎无法弄清楚如何保持我的转变。

4

2 回答 2

0

类似的问题已在此处得到解答:UIView rotates while scaling using CGAffineTransformMakeScale

在我看来,由于您是previewLayer手动旋转,因此您需要相应地调整识别器,因为您正在通过缩放覆盖变换。发生这种情况是因为转换是绝对的,或者至少我是这么认为的。

如何使用CGAffineTransformScale(正如我已链接的问题中所建议的那样),您可以在哪里传递 的原始转换previewLayer以及您想要的比例?

编辑这里是这个问题的文档:https://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CGAffineTransform/Reference/reference.html 在那里你可以看到这CGAffineTransformMakeScale是用于创建转换,并且CGAffineTransformScale改变它们,我相信这就是你所需要的。

编辑2

[UIView animateWithDuration:.25 animations:^{
   self.previewLayer.transform = CGAffineTransformScale(self.previewLayer.transform, effectiveScale, effectiveScale);
                 }];
于 2013-04-02T07:58:36.543 回答
0

解决了!

发现这个:“......改变预览层的方向不会改变图片的捕获方式。因为AVCaptureConnection's setVideoOrientation:需要使用该方法。”

..还有这个

[captureVideoPreviewLayer.connection setVideoOrientation:self.interfaceOrientation];

所以我替换了这个:

[self rotateToInterfaceOrientation:(UIInterfaceOrientation)[UIDevice currentDevice].orientation];

有了这个:

[self.previewLayer.connection setVideoOrientation:[UIDevice currentDevice].orientation];

现在它在横向模式下一切正常+捏缩放。:)

于 2013-04-04T00:45:33.487 回答