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.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With