返回函数内部无效 [英] Void Inside of Return function

查看:212
本文介绍了返回函数内部无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个用于点击图片的自定义类。在里面,我想创建一个 clickPicture 函数,它返回一个 UIImage 。但是, captureStillImageAsynchronously 是一个 void 。我怎样才能返回我从那里收到的图像?谢谢。

  func clickPicture() - > UIImage的?如果让videoConnection = stillImageOutput?.connection(withMediaType:AVMediaTypeVideo){

videoConnection.videoOrientation = .portrait
stillImageOutput?.captureStillImageAsynchronously(from:videoConnection, completionHandler:{(sampleBuffer,error) - >无效

如果sampleBuffer!= nil {

let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
let dataProvider = CGDataProvider(data:imageData!)
let cgImageRef = CGImage(jpegDataProviderSource:dataProvider !, decode:nil,shouldInterpolate:true,intent:.defaultIntent)
$ b $ image image = UIImage(cgImage: cgImageRef !, scale:1,orientation:.right)

return image //在void函数中出现意外的非void返回值

}
return nil //无效

中意外的非void返回值})

}

return nil
}


中的意外的nil发现后,这是未受挑战的#2 Swift问题。



这个方法描述得非常好:
$ b


捕捉静止图像异步

您不能从包含异步任务的方法返回任何内容。

你需要一个完成块:

  func clickPicture(完成:(UIImage?) - > Void){

guard让videoConnection = stillImageOutput?.connection(withMediaType:AVMediaTypeVideo)else {completion(nil)}

videoConnection.videoOrientation = .portrait
stillImageOutput?.captureStillImageAsynchronously(from:videoConnection ,completionHandler:{(sampleBuffer,error) - > void in

guard let buffer = sampleBuffer else {completion(nil)}

let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer )
let dataProvider = CGDataProvider(data:imageData!)
let cgImageRef = CG Image(jpegDataProviderSource:dataProvider !, decode:nil,shouldInterpolate:true,intent:.defaultIntent)

let image = UIImage(cgImage:cgImageRef !, scale:1,orientation:.right)

完成(图片)

})
}

然后调用它:

  clickPicture {image in 
if unwrappedImage = image {
//用unwrappedImage做一些事情
}
}


I am trying to create a custom class for clicking pictures. Inside of it, I would like to create a clickPicture function that returns a UIImage. However, the captureStillImageAsynchronously is a void. How can I return the image I receive from that? Thanks.

func clickPicture() -> UIImage? {

    if let videoConnection = stillImageOutput?.connection(withMediaType: AVMediaTypeVideo) {

        videoConnection.videoOrientation = .portrait
        stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (sampleBuffer, error) -> Void in

            if sampleBuffer != nil {

                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                let dataProvider = CGDataProvider(data: imageData!)
                let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)

                let image = UIImage(cgImage: cgImageRef!, scale: 1, orientation: .right)

                return image //Unexpected non-void return value in void function

            }
            return nil //Unexpected non-void return value in void

        })

    }

    return nil
}

解决方案

That's the unchallenged #2 Swift question after unexpected nil found while unwrapping an optional.

The method describes pretty well what is does :

capture still image asynchronously.

You cannot return anything from a method which contains an asynchronous task.
You need a completion block:

func clickPicture(completion:(UIImage?) -> Void) {

    guard let videoConnection = stillImageOutput?.connection(withMediaType: AVMediaTypeVideo)  else { completion(nil) }

    videoConnection.videoOrientation = .portrait
    stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (sampleBuffer, error) -> Void in

        guard let buffer = sampleBuffer else { completion(nil) }

        let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)
        let dataProvider = CGDataProvider(data: imageData!)
        let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)

        let image = UIImage(cgImage: cgImageRef!, scale: 1, orientation: .right)

        completion(image) 

    })
}

and call it this way:

clickPicture { image in 
   if unwrappedImage = image {
     // do something with unwrappedImage
   } 
}

这篇关于返回函数内部无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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