我正在使用这段代码:
func capturePhoto(blockCompletion: @escaping blockCompletionCapturePhoto) {
guard let connectionVideo = self.stillCameraOutput.connection(withMediaType: AVMediaTypeVideo) else {
blockCompletion(nil, nil)
return
}
connectionVideo.videoOrientation = AVCaptureVideoOrientation.orientationFromUIDeviceOrientation(orientation: UIDevice.current.orientation)
self.stillCameraOutput.captureStillImageAsynchronouslyFromConnection(connectionVideo) { (sampleBuffer: CMSampleBuffer!, err: NSError!) -> Void in
if let err = err {
blockCompletion(image: nil, error: err)
}
else {
if let sampleBuffer = sampleBuffer, let dataImage = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer) {
let image = UIImage(data: dataImage)
blockCompletion(image: image, error: nil)
}
else {
blockCompletion(image: nil, error: nil)
}
}
}
}
它在 Swift 2.0 中工作正常,但转换后就不再工作了。
这行:
self.stillCameraOutput.captureStillImageAsynchronouslyFromConnection(connectionVideo) { (sampleBuffer: CMSampleBuffer!, err: NSError!) -> Void in
给我以下错误:
无法将类型“(CMSampleBuffer!, NSError!) -> Void”的值转换为预期参数类型“((CMSampleBuffer?, Error?) -> Void)!”
我已经尝试过一些事情但无法解决。
希望有人能帮助我。
什么错误
无法将类型“(CMSampleBuffer!, NSError!) -> Void”的值转换为预期参数类型“((CMSampleBuffer?, Error?) -> Void)!”
基本上是说你的论点类型错误((CMSampleBuffer!, NSError!) -> Void
)虽然它应该是这种类型((CMSampleBuffer?, Error?) -> Void)!
.
为了实现这一点,请尝试使用此代码,它应该自动使您的块符合正确的类型:
self.stillCameraOutput.captureStillImageAsynchronouslyFromConnection(connectionVideo) { sampleBuffer, error in
//do stuff with your sample buffer, don't forget to handle errors
}
它看起来像一个奇怪的类型,但我认为这是苹果在将此代码从 ObjC 迁移到 Swift 1、Swift 2、Swift 3 时犯的一个小错误。
我还没有测试过这段代码,但我认为它应该有效,让我知道它是否真的有效!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)