想要使用AVFoundation框架在iphone相机中进行缩放功能

时间:2013-07-10 07:14:37

标签: iphone ios objective-c xcode avfoundation

我想使用 UISlider 缩放相机。

我通过调整 AVCaptureVideoPreviewLayer AffineTransform 成功完成了这项工作。

这是代码

-(void)sliderAction:(UISlider*)sender{
    CGAffineTransform affineTransform = CGAffineTransformMakeTranslation(sender.value, sender.value);
    affineTransform = CGAffineTransformScale(affineTransform, sender.value, sender.value);
    affineTransform = CGAffineTransformRotate(affineTransform, 0);
    [CATransaction begin];
    [CATransaction setAnimationDuration:.025];
     //previewLayer is object of AVCaptureVideoPreviewLayer
    [[[self captureManager]previewLayer] setAffineTransform:affineTransform];
    [CATransaction commit];
}

但是当我捕获它时,我会得到非缩放的图像对象。

2 个答案:

答案 0 :(得分:3)

回复有点迟了。但我正在回复以供将来参考。 实际上,您在代码中所做的只是您已经更改了预览图层的缩放系数而不是基础输出连接。但是,要使缩放最初反映在捕获的输出上,您必须将该因子也放在输出连接中。您可以使用类似于下面的内容:

-(void)sliderAction:(UISlider*)sender
{
    AVCaptureConnection* connection = [self.photoOutput connectionWithMediaType:AVMediaTypeVideo]; // photoOutput is a AVCaptureStillImageOutput object, representing a capture session output with customized preset

    CGAffineTransform affineTransform = CGAffineTransformMakeTranslation(sender.value, sender.value);
    affineTransform = CGAffineTransformScale(affineTransform, sender.value, sender.value);
    affineTransform = CGAffineTransformRotate(affineTransform, 0);
    [CATransaction begin];
    [CATransaction setAnimationDuration:.025];
     //previewLayer is object of AVCaptureVideoPreviewLayer
    [[[self captureManager]previewLayer] setAffineTransform:affineTransform];
    if (connection) {
        connection.videoScaleAndCropFactor = sender.value;
    }
    [CATransaction commit];
}

它应该可以解决问题。

理想情况下,您不应在connection.videoScaleAndCropFactor例程中执行Slider更改,并且应将代码放在原始捕获例程中,并使用滑块的瞬时值将其设置为仅一次,就在之前调用captureStillImageAsynchronouslyFromConnection方法。

希望有所帮助:)

答案 1 :(得分:0)

首先,您的代码仅缩放图层内容而不是CMSampleBuffer。您下一步的工作是从CVPixelBuffer CMSampleBuffer开始缩放,并将缩放CMSampleBuffer保存到AVWriter。您可以使用Accelerate.framework来扩展CVPixelBuffer