gpt4 book ai didi

ios - Swift 3 照片拍摄

转载 作者:搜寻专家 更新时间:2023-10-31 22:13:02 25 4
gpt4 key购买 nike

我正在使用这段代码:

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/

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