使用AVFoundation录制视频时自定义相机的缩放功能如何实现?

6
在最新的iOS 7.1中,原生相机应用程序可以在录制视频时进行缩放,并且保存在“照片”中的视频确实显示了缩放效果。
现在,我正在使用AVFoundation来实现自定义视频。我可以通过使用videoMaxScaleAndCropFactor修改AVCaptureVideoPreviewLayer来在录制视频时进行缩放。然而,保存的视频没有显示出缩放效果。
是否有任何提示可以实现此功能?

抱歉我写在评论里,但如果你有什么线索一定要在这里写下来。感谢您的投票。 :) - Mitul Bhadeshiya
2个回答

3

试试这个:

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:


我希望我能够多次点赞你的回答,它完全有效。谢谢,你刚刚救了我的一天。 - Rana

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接