gpt4 book ai didi

ios - swift : Error on value of type 'Data' to type 'Data'

转载 作者:行者123 更新时间:2023-11-30 11:01:24 25 4
gpt4 key购买 nike

我有一些使用 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 allocate value of type 'Data?'输入“Data?”无法将“Data”类型的值转换为预期的参数类型“Data”这个问题是否是Swift版本不同造成的?

注意:这个“另一个项目”部署目标是 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 及更高版本

希望对你有帮助...

关于ios - swift : Error on value of type 'Data' to type 'Data' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53321678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com