Steve Graff
Steve Graff

Reputation: 308

AVCapture and zooming of previewLayer in Swift

I have a camera app which allows the user to both take pictures and record video. The iPhone is attached to a medical otoscope using an adapter, so the video that is captured is very small (about the size of a dime). I need to be able to zoom the video to fill the screen, but have not been able to figure out how to do so.

I found this answer here on SO that uses ObjC but have not had success in translating it to Swift. I am very close but am getting stuck. Here is my code for handling a UIPinchGestureRecgoznier:

    @IBAction func handlePinchGesture(sender: UIPinchGestureRecognizer) {

    var initialVideoZoomFactor: CGFloat = 0.0

    if (sender.state == UIGestureRecognizerState.began) {
        initialVideoZoomFactor = (captureDevice?.videoZoomFactor)!
    } else {
        let scale: CGFloat = min(max(1, initialVideoZoomFactor * sender.scale), 4)

        CATransaction.begin()
        CATransaction.setAnimationDuration(0.01)
        previewLayer?.transform = CGAffineTransform(scaleX: scale, y: scale)
        CATransaction.commit()

        if ((captureDevice?.lockForConfiguration()) != nil) {
            captureDevice?.videoZoomFactor = scale
            captureDevice?.unlockForConfiguration()
        }

    }
}

This line...

previewLayer?.transform = CGAffineTransform(scaleX: scale, y: scale)

... gives me the error 'Cannot assign value of type 'CGAffineTransform' to type 'CGTransform3D'. I'm trying to figure this out but my attempts to fix this have been unfruitful.

Upvotes: 2

Views: 1717

Answers (1)

Steve Graff
Steve Graff

Reputation: 308

Figured it out: Changed the problematic line to:

previewLayer?.setAffineTransform(CGAffineTransform(scaleX: scale, y: scale))

Upvotes: 3

Related Questions