Reputation: 1658
I want to zoom a camera by using UISlider .
I have done it successfully by adjusting the AffineTransform of AVCaptureVideoPreviewLayer.
Here is code of it
-(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];
}
but when I capture it, I am getting non- zoomed object of image.
Upvotes: 7
Views: 2976
Reputation: 5386
It's a bit late to reply. But I am replying for future reference. Actually what you have done in you code is only that you have changed the zoom factor of the preview layer and not the underlying output connection. But for the zoom to be originally reflected on the captured output, you must put the factor in your output connection as well. You can use something similar to below:
-(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];
}
And it should do the trick.
Ideally, you shouldn't perform the connection.videoScaleAndCropFactor
change in your Slider
routine and should place the code in your original capture routine and set it only once with the momentary value of the slider, just before calling captureStillImageAsynchronouslyFromConnection
method.
Hope it helps :)
Upvotes: 3
Reputation: 3526
First your code zooming only layer contents not CMSampleBuffer
. Your next work around is to make a scale on CVPixelBuffer
from CMSampleBuffer
and save scaled CMSampleBuffer
to AVWriter
. You can use Accelerate.framework to scale CVPixelBuffer
.
Upvotes: 0