avfoundationswift5avcapture

How to Apply a Scale transform to an AVCaptureVideoPreviewLayer


I have an AVCaptureVideoPreviewLayer named previewlayer that is set to a UIImageView named VideoOut that was implemented using Storyboard

The code I used to connect them was as follows

    previewlayer.frame = VideoOut.bounds 

I then wanted to add a "zoom in" button that would scale VideoOut by a factor of 0.25 each time the "Zoom In" button was pressed. The method I tried to use is shown below

    @IBAction func Zooma(_ sender: Any) {
    UIView.animate(withDuration: 0.0, delay: 0.0, options: [], animations: {
        self.VideoOut.transform = CGAffineTransform(scaleX: 1.25, y: 1.25)
    }, completion: nil)

Though this does nothing in terms of scaling the ImageView with the video feed. Although when I use the same code on an Imageview not connected to the AVCaptureVideoPreviewLayer the line of code works as intended.

Any advice or insight would be much appreciated


Solution

  • If you want a smooth video zoom, try a zoom ramp on the AVCaptureDevice object that the capture layer originates from: ramp(toVideoZoomFactor: 1.25, withRate: 0.25)