0

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

Ricardo Milos
  • 157
  • 1
  • 1
  • 15

3 Answers3

0

Check if the error is not caused by the iOS version of your device.

Keep in mind that

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

Is available for iOS11 and later for iOS10 and earlier you should use

optional func photoOutput(_ output: AVCapturePhotoOutput,
 didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?,
previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?,
resolvedSettings: AVCaptureResolvedPhotoSettings, 
bracketSettings: AVCaptureBracketedStillImageSettings?, 
error: Error?)

Also

fileDataRepresentation()

Is available for iOS11 and later

Hope it helps...

FedeH
  • 1,343
  • 18
  • 24
0

Someone has override the Data type. You can bypass this using :

var photoData: Swift.Data?

CZ54
  • 5,488
  • 1
  • 24
  • 39
0

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)
}
Ricardo Milos
  • 157
  • 1
  • 1
  • 15