如何使用AVFoundation将预览(缩放)图像保存为照片?

时间:2016-10-03 09:34:07

标签: ios swift avfoundation swift3 avcapture

所以,我已经如何使用CATransform3DMakeScale(2.4,2.4,2.4)制作缩放效果,但现在我在尝试保存Zoome预览消息时遇到了问题(代码就像我缩放一样):

 // init camera device
    let captureDevice : AVCaptureDevice? = initCaptureDevice()
    print("camera was initialized")
    // Prepare output
    initOutput()
    if (captureDevice != nil) {
        let deviceInput : AVCaptureInput? = initInputDevice(captureDevice: captureDevice!)
        if (deviceInput != nil) {
            initSession(deviceInput: deviceInput!)
            // Preview Size
            let previewLayer: AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: self.session)
            previewLayer.frame = self.view.bounds
            previewLayer.transform = CATransform3DMakeScale(2.4, 2.4, 2.4)
             imagePreviewScale = previewLayer.contentsScale
            self.view.layer.addSublayer(previewLayer)
            self.session?.startRunning()

现在我尝试保存预览的Zoomed图像,如下所示:

 let videoConnection : AVCaptureConnection? = self.imageOutput?.connection(withMediaType: AVMediaTypeVideo)
    if (videoConnection != nil) {

        videoConnection?.videoScaleAndCropFactor = imagePreviewScale

        self.imageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (imageDataSampleBuffer, error) -> Void in
            if (imageDataSampleBuffer != nil) {
                                    // Capture JPEG

                let imageData : NSData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer) as NSData
                // JPEG
                let image = UIImage(data: imageData as Data)

并添加了一行

 imagePreviewScale = previewLayer.contentsScale

但仍然没有任何反应,请任何人都能告诉我如何保存The Exactly Zoomed图片?

1 个答案:

答案 0 :(得分:1)

问题是previewLayer.contentsScale为1,因此您将videoScaleAndCropFactor设置为1。

您需要将其设置为

videoConnection?.videoScaleAndCropFactor = 2.4