ios 如何测试空的 UIImageView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6325849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:19:22  来源:igfitidea点击:

How to test for an empty UIImageView?

iphoneios

提问by jini

I have the following code where I want to see if a particular UIImageView(image) is set. If not then I want to display an error message.

我有以下代码,我想查看是否设置了特定UIImageView(图像)。如果没有,那么我想显示一条错误消息。

if (image==nil) {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"No Image Selected" 
                                                                 delegate:nil 
                                                        cancelButtonTitle:@"OK" 
                                                   destructiveButtonTitle:nil 
                                                        otherButtonTitles:nil];

        [actionSheet showInView:[[self view]window]];
        [actionSheet autorelease];
    }

回答by Wilbur Vandrsmith

imageView.image == nil? Or, to check for empty images, CGSizeEqualToSize(imageView.image.size, CGSizeZero).

…… imageView.image == nil?或者,要检查空图像,CGSizeEqualToSize(imageView.image.size, CGSizeZero).

回答by MikeJfromVA

It may be tricky to figure out if (UIImageView *)imagehas been initialized with an image. You may want to check the image property:

确定是否(UIImageView *)image已使用图像初始化可能很棘手。您可能需要检查图像属性:

if (image.image == nil)

This will work in the case where image.image==nil(UIImageViewinitialized by image not set) AND in the case where image==nil(since messages sent to nil return nil).

这将适用于image.image==nilUIImageView由未设置的图像初始化)和image==nil(因为消息发送到 nil return nil)的情况。

Beware local variables as well: Just because you never alloc/initimage doesn't mean it will be nil.

alloc/init也要注意局部变量:仅仅因为你从不成像并不意味着它会为零。