Swift : 类型 'Data' 的值错误以键入 'Data'

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

我有一些使用 AVKit 和 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)
        }
    }
}

当我制作独立项目时,这段代码编译没有问题。但是当我尝试申请另一个项目时,出现了一些错误,例如 Cannot assign value of type 'Data?' to type 'Data?'Cannot convert value of type 'Data' to expected argument type 'Data' 此问题是否由不同 Swift 版本引起?

注意: 此 "another project" 部署目标是 iOS 10 & swift 3 并且将 func capture 用于 didFinishCaptureForResolvedSettings 而不能使用 func photoOutput 我的独立项目是 运行 使用 Swift 4,部署目标是 iOS 11.3

检查错误是否不是由您设备的 iOS 版本引起的。

请记住

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

适用于 iOS11,之后适用于 iOS10,而更早的版本你应该使用

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

还有

fileDataRepresentation()

适用于 iOS11 及更高版本

希望对您有所帮助...

有人覆盖了 Data 类型。您可以使用 :

绕过它

var photoData: Swift.Data?

通过使用解决 func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings:AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, 错误:错误?){

    // 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)
}