iosswiftxcodecameraios-camera

Swift : Error on value of type 'Data' to type 'Data'


I have some function for Camera using AVKit and AVCapturePhotoCaptureDelegate.

import UIKit 
import AVKit

class CaptureImageClass: NSObject, AVCapturePhotoCaptureDelegate {

    var photoData: Data?

    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {

        if let error = error {
            print("Error capturing photo: \(error)")
        } else {
            photoData = photo.fileDataRepresentation() //Cannot assign value of type 'Data?' to type 'Data?'
        }
    }

    func capture(_ output: AVCapturePhotoOutput, didFinishCaptureForResolvedSettings resolvedSettings: AVCaptureResolvedPhotoSettings, error: Error?) {
        guard let photoData = photoData else {
            print("No photo data resource")
            return
        }
        let capturedImage = UIImage.init(data: photoData , scale: 1.0) //Cannot convert value of type 'Data' to expected argument type 'Data'
        if let image = capturedImage {
            UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
        }
    }
}

this code compiled without problem when i make standalone project. But when i try to apply to another project, it have some error like Cannot assign value of type 'Data?' to type 'Data?' and Cannot convert value of type 'Data' to expected argument type 'Data' Is this problem caused by diffrent Swift version or not?

Note: this "another project" deployment target is iOS 10 & swift 3 and using func capture for didFinishCaptureForResolvedSettings and cant using func photoOutput My standalone project is running using Swift 4 and for the deployment target is iOS 11.3


Solution

  • Solved by using func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {

        // Make sure we get some photo sample buffer
        guard error == nil,
            let photoSampleBuffer = photoSampleBuffer else {
                print("Error capturing photo: \(String(describing: error))")
                return
        }
        // Convert photo same buffer to a jpeg image data by using // AVCapturePhotoOutput
        guard let imageData =
            AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else {
                return
        }
    
        let dataProvider = CGDataProvider(data: imageData as CFData)
    
        let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.absoluteColorimetric)
    
    
        let image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.right)
    
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
    }