自定义相机使用AVFoundation录制视频时放大/缩小

时间:2014-03-31 04:10:50

标签: ios video avfoundation zoom recording

在最新的iOS 7.1中 原生相机应用可以在录制视频时放大/缩小,而照片中保存的视频确实显示放大/缩小效果。

现在,我正在使用AVFoundation来实现自定义视频。 通过使用videoMaxScaleAndCropFactor修改AVCaptureVideoPreviewLayer,我可以在录制视频时放大/缩小。但是,保存的视频不会显示放大/缩小效果。

是否有任何提示实现此功能????

2 个答案:

答案 0 :(得分:3)

我也在寻找那个。下面的链接提供了缩放视频的解决方案。

http://www.iphonelife.com/blog/87/imaging-video-guru-reporting-lossless-video-zooming-ios7

缩放视频的逻辑是:

int selectedAVCaptureDeviceFormatIdx = 15;

[self.videoDevice lockForConfiguration:nil];

AVCaptureDeviceFormat* currdf = [self.videoDevice.formats objectAtIndex:selectedAVCaptureDeviceFormatIdx];
self.videoDevice.activeFormat = currdf;
if (selectedAVCaptureDeviceFormatIdx==12 || selectedAVCaptureDeviceFormatIdx==13)
    self.videoDevice.activeVideoMaxFrameDuration = CMTimeMake(1,60);

NSLog(@"%f", self.videoDevice.activeFormat.videoMaxZoomFactor);
NSLog(@"videoZoomFactorUpscaleThreshold: %f", self.videoDevice.activeFormat.videoZoomFactorUpscaleThreshold);

// If you want to zoom to the threshold of possible zooming before binning occurs
self.videoDevice.videoZoomFactor = videoDevice.activeFormat.videoZoomFactorUpscaleThreshold;
// If you want to set your own zoom factor
//self.videoDevice.videoZoomFactor = 3.0f;// here zoom given in CGFloat like 1, 2, 3, 4, 5.

[self.videoDevice unlockForConfiguration:nil];

答案 1 :(得分:2)

试试这个:

float zoomLevel = 2.0f;
float zoomRate = 2.0f;
if ([device respondsToSelector:@selector(rampToVideoZoomFactor:)]
    && device.activeFormat.videoMaxZoomFactor >= zoomLevel) {
  if ([[device lockForConfiguration:nil]) {
  [device rampToVideoZoomFactor:zoomLevel withRate:zoomRate];
  [device unlockForConfiguration];
  }
}

这提供了平滑的变焦。对于即时缩放(例如,响应由UISlider触发大量数据而导致的微小变化),请使用setVideoZoomFactor:代替rampToVideoZoomFactor:withRate: