Swift 3 拍照 [英] Swift 3 photo capturing

查看:30
本文介绍了Swift 3 拍照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这段代码:

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 中运行良好,但转换后不再运行.这一行:

It worked fine in Swift 2.0, but after conversion it's not working anymore. This line:

self.stillCameraOutput.captureStillImageAsynchronouslyFromConnection(connectionVideo) { (sampleBuffer: CMSampleBuffer!, err: NSError!) -> Void in 

给我以下错误:

无法将 '(CMSampleBuffer!, NSError!) -> Void' 类型的值转换为预期的参数类型 '((CMSampleBuffer?, Error?) -> Void)!'

Cannot convert value of type '(CMSampleBuffer!, NSError!) -> Void' to expected argument type '((CMSampleBuffer?, Error?) -> Void)!'

我已经尝试了一些事情,但无法解决.希望有人能帮助我.

I've already tried some things but can't get it solved. Hopefully someone can help me.

推荐答案

什么错误

无法将 '(CMSampleBuffer!, NSError!) -> Void' 类型的值转换为预期的参数类型 '((CMSampleBuffer?, Error?) -> Void)!'

Cannot convert value of type '(CMSampleBuffer!, NSError!) -> Void' to expected argument type '((CMSampleBuffer?, Error?) -> Void)!'

基本上是说你的参数是错误的类型 ((CMSampleBuffer!, NSError!) -> Void) 而它应该是 ((CMSampleBuffer?, Error?) -> Void)!.

basically says is that your argument is of the wrong type ((CMSampleBuffer!, NSError!) -> Void) while it should be of the type ((CMSampleBuffer?, Error?) -> Void)!.

要实现这一点,请尝试使用此代码,它应该会自动使您的块符合正确的类型:

To achieve this, try using this code, it should automatically make your block conform to the right type:

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 时在某个地方犯的一个小错误.
我还没有测试过这段代码,但我认为它应该有效,如果真的有效,请告诉我!

It looks like a weird type but I think it's a little error Apple made somewhere while migrating this code from ObjC to Swift 1 to Swift 2 to Swift 3.
I haven't tested this code, but I think it should work, let me know if it actually did!

这篇关于Swift 3 拍照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆