简体   繁体   中英

Custom Camera zoom in/out while recording video using AVFoundation

In the latest iOS 7.1 the native camera app can zoom in/out while recording video, and the video saved in the Photos indeed shows the zoom in/out effect.

now, I am using AVFoundation to implement custom Video. I can zoom in/out while recording video by using videoMaxScaleAndCropFactor to modify AVCaptureVideoPreviewLayer. However, the saved video doesn't show the zoom in/out effect.

Is there any hint to implement this function ????

I am also searching for that. The link below gives a solution for zooming video.

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

The logic for zooming video is:

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

Try this:

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

That gives a smooth zoom. For an instant zoom (eg responding to small changes caused by a UISlider firing off lots of data) use setVideoZoomFactor: instead of rampToVideoZoomFactor:withRate: .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM