UIPinchGestureRecognizer缩放AVCaptureConnection;不缩小

时间:2014-07-12 00:32:19

标签: ios objective-c avfoundation avcapturesession uipinchgesturerecognizer

我有一个问题,我有一个UIPinchGestureRecognizer,我用来扩展我的相机预览和相应的AVCaptureConnection进出。第一次放大和缩小没有问题,直到你完成捏缩放后手指抬起。如果我尝试向内捏(缩放回正常1.0刻度),手势不会重置预览并且AVCaptureConnection缩小,因此预览和AVCaptureConnection保持相同的大小。

我怎么能解决这个问题呢? if语句确保比例不低于1.0,因为我们不允许将比例设置为低于1.0的AVCaptureConnection。

- (void)zoomPreview:(UIPinchGestureRecognizer *)recognizer
{
    CGFloat scale = recognizer.scale;

    NSLog(@"Scale: %f",scale);

    if (scale >= 1.0)
    {
        [[self.stillImageOutput.connections objectAtIndex:0] setVideoScaleAndCropFactor:scale];

        self.previewView.transform = CGAffineTransformMakeScale(scale, scale);
    }
}

1 个答案:

答案 0 :(得分:0)

通过反复试验解决了自己:

@property (nonatomic, assign) CGFloat lastScale;
@property (nonatomic, assign) CGFloat currentScale;

- (void)zoomPreview:(UIPinchGestureRecognizer *)recognizer
{
    self.currentScale += recognizer.scale - self.lastScale;
    self.lastScale = recognizer.scale;

    if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled)
    {
        self.lastScale = 1.0;
    }

    if (self.currentScale < 1.0)
    {
        self.currentScale = 1.0;
    }

    if (self.currentScale > 5.0)
    {
        self.currentScale = 5.0;
    }

    if (self.currentScale >= 1.0 && self.currentScale <= 5.0)
    {
        [[self.stillImageOutput.connections objectAtIndex:0]setVideoScaleAndCropFactor:self.currentScale];

        self.previewView.transform = CGAffineTransformMakeScale(self.currentScale, self.currentScale);
    }
}