简体   繁体   English

预期返回“ UIImage”的函数中缺少返回

[英]Missing return in a function expected to return 'UIImage'

I am a beginner of Swift. 我是Swift的初学者。
Currently, I am using AVFoundation to create camera applications. 目前,我正在使用AVFoundation创建相机应用程序。
I am coding from Processing A to Processing B. 我正在从处理A编码到处理B。
However, the following error was displayed. 但是,显示以下错误。
Error: 错误:

Missing return in a function expected to return 'UIImage'. 预期返回“ UIImage”的函数中缺少返回。

I do not know how to use return in the switch statement. 我不知道如何在switch语句中使用return。

//Processing A
func captureImage(_ sampleBuffer: CMSampleBuffer) -> UIImage {
    .....
    switch self.input.device.position {
        case .front:
            let resultImage = UIImage(cgImage: imageRef, scale: 1.0, orientation: UIImageOrientation.down)
            return resultImage
        case .back:
            let resultImage = UIImage(cgImage: imageRef, scale: 1.0, orientation: UIImageOrientation.up)
            return resultImage
        default:
            print("error")
    }
}

在此处输入图片说明

This error occurs when not all paths of your code return a value. 当并非代码的所有路径都返回值时,将发生此错误。 If you say your method returns a UIImage , it must always do . 如果您说您的方法返回UIImage ,则它必须始终这样做

Let's take a closer look at the implementation of captureImage : 让我们仔细看看captureImage的实现:

func captureImage(_ sampleBuffer: CMSampleBuffer) -> UIImage {
    .....
    switch self.input.device.position {
        case .front:
            let resultImage = UIImage(cgImage: imageRef, scale: 1.0, orientation: UIImageOrientation.down)
            return resultImage
        case .back:
            let resultImage = UIImage(cgImage: imageRef, scale: 1.0, orientation: UIImageOrientation.up)
            return resultImage
        default:
            print("error")
    }
 }

We can see that if input.device.position is .front or .back , the method returns a value. 我们可以看到,如果input.device.position.front.back ,则该方法返回一个值。 However, what if input.device.position is neither of those values? 但是,如果input.device.position都不是这些值怎么办? The method will just print "error" and return nothing. 该方法将仅显示“错误”,但不返回任何内容。 That's not acceptable is it? 那是不能接受的吗?

You might say, " I'm sure that input.device.position can only be either front or back in this situation. It can't be anything else!" 您可能会说:“在这种情况下,我确定input.device.position只能在正面或背面。它不能是其他任何东西!” Well, the compiler isn't sure about that. 好吧,编译器对此不确定。 It just sees that there are other possible values for input.device.position . 它只是看到input.device.position还有其他可能的值。

In this case, I suggest that you just do fatalError() when it's neither of those values. 在这种情况下,我建议您只在两者都不是时执行fatalError() It will just crash your app. 它将使您的应用程序崩溃。 If you do this then the method doesn't need to return anything. 如果执行此操作,则该方法无需返回任何内容。 The app is crashed after all. 该应用程序毕竟崩溃了。

In each case you need to return UIImage. 在每种情况下,您都需要返回UIImage。 If case is default just return a empty UIImage. 如果大小写为默认值,则返回一个空的UIImage。

func captureImage(_ sampleBuffer: CMSampleBuffer) -> UIImage {
.....
switch self.input.device.position {
    case .front:
        let resultImage = UIImage(cgImage: imageRef, scale: 1.0, orientation: UIImageOrientation.down)
        return resultImage
    case .back:
        let resultImage = UIImage(cgImage: imageRef, scale: 1.0, orientation: UIImageOrientation.up)
        return resultImage
    default:
        print("error")
        return UIImage()
  }
}

See Sweepers answer for a solid explanation of why you're running into this error. 有关为什么会遇到此错误的详细说明,请参见Sweepers答案。 However, I would simply return a blank UIImage inside your default case after printing 'error'. 但是,在打印“错误”后,我只会在默认情况下返回一个空白的UIImage。 return UIImage()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM