如何确定UIImage是否为空?

时间:2021-08-26 21:18:25

How do I check to see if a UIImage is empty?

如何检查UIImage是否为空?

class UserData {
    ...
    var photo: UIImage = UIImage()
}

My ViewController code looks like:

我的ViewController代码如下所示:

var userData = UserData()
    ...

func preparePhoto(){
        if (self.userData.photo == nil) {
            ...
        }else{
            ...
        }
    }

self.userData.photo == nil won't work in Swift.

self.userData.photo == nil在Swift中不起作用。

Xcode says: UIImage is not convertible to MirrorDisposition

Xcode说:UIImage不能转换为MirrorDisposition

1 个解决方案

#1


9  

self.userData.photo will never be nil, so the question is pointless.

self.userData.photo永远不会是零,所以这个问题毫无意义。

The reason is that you have declared photo as a UIImage. That is not the same as a UIImage? - an Optional wrapping a UIImage. Only an Optional can be nil in Swift. But, as I just said, photo is not an Optional. Therefore there is nothing to check. That is why Swift stops you when you try to perform such a check.

原因是您已将照片声明为UIImage。那和UIImage不一样吗? - 包装UIImage的可选项。 Swift中只有一个Optional可以是nil。但是,正如我刚才所说,照片不是可选的。因此无需检查。这就是为什么当你试图执行这样的检查时Swift会阻止你。

So, what to do? I have two possible suggestions:

那么该怎么办?我有两个可能的建议:

  • My actual recommendation for solving this is that you do type photo as a UIImage? and set it initially to nil (actually, it is implicitly nil from the start). Now you can check for nil to see whether an actual image has been assigned to it.

    我解决此问题的实际建议是你输入照片作为UIImage吗?并将其初始设置为nil(实际上,它从一开始就隐含为nil)。现在,您可以检查nil以查看是否已为其分配实际图像。

    But keep in mind that you then will have to remember to unwrap photo when you want to use it for anything! (You could instead type photo as an implicitly unwrapped optional, UIImage!, to avoid that constant unwrapping, but I am not as keen on that idea.)

    但请记住,当您想要将照片用于任何事情时,您必须记得打开照片! (你可以改为将照片键入为隐式展开的可选UIImage !,以避免不断展开,但我并不热衷于这个想法。)

  • An alternative possibility would be to examine the size of the image. If it is zero size, it is probably an empty image in the original sense of your question!

    另一种可能性是检查图像的大小。如果它是零大小,它可能是你问题的原始意义上的空图像!

#1


9  

self.userData.photo will never be nil, so the question is pointless.

self.userData.photo永远不会是零,所以这个问题毫无意义。

The reason is that you have declared photo as a UIImage. That is not the same as a UIImage? - an Optional wrapping a UIImage. Only an Optional can be nil in Swift. But, as I just said, photo is not an Optional. Therefore there is nothing to check. That is why Swift stops you when you try to perform such a check.

原因是您已将照片声明为UIImage。那和UIImage不一样吗? - 包装UIImage的可选项。 Swift中只有一个Optional可以是nil。但是,正如我刚才所说,照片不是可选的。因此无需检查。这就是为什么当你试图执行这样的检查时Swift会阻止你。

So, what to do? I have two possible suggestions:

那么该怎么办?我有两个可能的建议:

  • My actual recommendation for solving this is that you do type photo as a UIImage? and set it initially to nil (actually, it is implicitly nil from the start). Now you can check for nil to see whether an actual image has been assigned to it.

    我解决此问题的实际建议是你输入照片作为UIImage吗?并将其初始设置为nil(实际上,它从一开始就隐含为nil)。现在,您可以检查nil以查看是否已为其分配实际图像。

    But keep in mind that you then will have to remember to unwrap photo when you want to use it for anything! (You could instead type photo as an implicitly unwrapped optional, UIImage!, to avoid that constant unwrapping, but I am not as keen on that idea.)

    但请记住,当您想要将照片用于任何事情时,您必须记得打开照片! (你可以改为将照片键入为隐式展开的可选UIImage !,以避免不断展开,但我并不热衷于这个想法。)

  • An alternative possibility would be to examine the size of the image. If it is zero size, it is probably an empty image in the original sense of your question!

    另一种可能性是检查图像的大小。如果它是零大小,它可能是你问题的原始意义上的空图像!