如何为视频添加捏缩放

时间:2019-02-13 14:18:29

标签: ios objective-c xcode avplayer pinchzoom

我使用AVFoundation库的AVCapture录制了视频,并将其保存在文档文件夹中。我可以在我的应用中使用AVPlayer播放录制的视频。现在,我正在尝试实现Pinch Zoom。因此它的工作原理与照片应用程序相同。我不想使用scrollview,因为我想录制ZOOMABLE视频。

为解决此问题,我有一个观点。在此视图中,我正在使用AVPlayer。因此,可能我需要在视图中添加 UIPinchGestureRecognizer ,之后我将进行缩放以进行查看,并且还将更改 AVCaptureDevice videoZoomFactor 的值。我尝试过,但有时我的视角更大,而录制的视频却变小。我要使其与照相相机应用程序相同。

-(void) handlePinchToZoomRecognizer:(UIPinchGestureRecognizer*)gestureRecognizer {
// NSLog(@"handlePinchToZoomRecognizer called");

if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
    // Reset the last scale, necessary if there are multiple objects with different scales
    lastScale = [gestureRecognizer scale];
}

/* if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
    [gestureRecognizer state] == UIGestureRecognizerStateChanged) {*/
if ([gestureRecognizer state] == UIGestureRecognizerStateChanged) {

    CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];

    // Constants to adjust the max/min values of zoom
    const CGFloat kMaxScale = 2.0;
    const CGFloat kMinScale = 1.0;

    //CGFloat newScale = 1 –  (lastScale – [gestureRecognizer scale]); // new scale is in the range (0-1)
    CGFloat newScale = 1 - (lastScale - [gestureRecognizer scale] );
    newScale = MIN(newScale, kMaxScale / currentScale);
    newScale = MAX(newScale, kMinScale / currentScale);
    CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
    [gestureRecognizer view].transform = transform;

    lastScale = [gestureRecognizer scale];  // Store the previous scale factor for the next pinch gesture call

    NSLog(@"handlePinchToZoomRecognizer %f", lastScale);

    if(lastScale > 0) {
        if (gestureRecognizer.state == UIGestureRecognizerStateChanged) {
            NSError *error = nil;
            if ([self.videoInputDevice lockForConfiguration:&error]) {
                //   CGFloat desiredZoomFactor = self.videoInputDevice.videoZoomFactor + atan2f(gestureRecognizer.velocity, pinchVelocityDividerFactor);
                // Check if desiredZoomFactor fits required range from 1.0 to activeFormat.videoMaxZoomFactor
                /*   self.videoInputDevice.videoZoomFactor = MAX(1.0, MIN(desiredZoomFactor, self.videoInputDevice.activeFormat.videoMaxZoomFactor));*/
                self.videoInputDevice.videoZoomFactor = lastScale;
                [self.videoInputDevice unlockForConfiguration];
            } else {
                NSLog(@"handlePinchToZoomRecognizer error: %@", error);
            }
        }
    }


}

}

0 个答案:

没有答案