我正在使用这段代码:
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
给我以下错误:
Cannot convert value of type '(CMSampleBuffer!, NSError!) -> Void' to expected argument type '((CMSampleBuffer?, Error?) -> Void)!'
我已经尝试了一些事情,但无法解决。 希望有人能帮助我。
最佳答案
什么错误
Cannot convert value of type '(CMSampleBuffer!, NSError!) -> Void' to expected argument type '((CMSampleBuffer?, Error?) -> Void)!'
基本上是说您的参数类型错误 ((CMSampleBuffer!, NSError!) -> Void
) 而它应该是类型 ((CMSampleBuffer?, Error? ) -> 无效)!
.
为此,请尝试使用此代码,它应该会自动使您的 block 符合正确的类型:
self.stillCameraOutput.captureStillImageAsynchronouslyFromConnection(connectionVideo) { sampleBuffer, error in
//do stuff with your sample buffer, don't forget to handle errors
}
它看起来像一个奇怪的类型,但我认为这是 Apple 在将这段代码从 ObjC 迁移到 Swift 1、Swift 2 再到 Swift 3 时犯的一个小错误。
我还没有测试过这段代码,但我认为它应该可以工作,如果真的可以,请告诉我!
关于ios - Swift 3 照片拍摄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39492339/