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

杜米特鲁·罗戈基纳鲁(Dumitru Rogojinaru)

因此,我已经解决了如何使用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()

现在,我尝试像这样保存预览的缩放图像:

 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

但是仍然没有任何反应,请任何人可以告诉我如何保存“精确缩放”的图片?

节奏拳头

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

您需要将其设置为

videoConnection?.videoScaleAndCropFactor = 2.4

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章