簡體   English   中英

如何使用 AVFoundation 將預覽(縮放)圖像保存為照片?

[英]How to save the previewed (Zoomed) image as Photo using AVFoundation?

所以,我已經了解了如何使用 CATransform3DMakeScale(2.4, 2.4, 2.4) 制作縮放效果,但現在我在嘗試保存縮放預覽消息時遇到問題(代碼與我縮放一樣):

 // 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.contentsScale為 1,因此您將videoScaleAndCropFactor設置為 1。

您需要將其設置為

videoConnection?.videoScaleAndCropFactor = 2.4

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM