如何查明图像是否存在于捆绑包中?

发布于 2024-09-05 00:45:32 字数 216 浏览 3 评论 0原文

我有一个 NSString 数组:

Flower
Car
Tree
Cat
Shoe

其中一些字符串具有与其关联的图像;有些则不然。我可以通过将 .png 附加到名称(例如 Flower.png)来构建图像名称。

在尝试将图像加载到视图中之前,如何检查该图像是否确实存在于捆绑包中?

I have an array of NSStrings:

Flower
Car
Tree
Cat
Shoe

Some of these strings have images associated with them; some don't. I can build an image name by appending .png to the name (e.g. Flower.png).

How do I check whether that image actually exists within the bundle before I try to load it into the view?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

十级心震 2024-09-12 00:45:32

这应该也可以工作,而且有点短:

if (flowerImage = [UIImage imageNamed:@"Flower.png"])
{
   ... // do something here
}
else
{   
   ... // image does not exist
}

imageNamed: 方法将在主包中查找 png 文件。

This should also work, and is a bit shorter:

if (flowerImage = [UIImage imageNamed:@"Flower.png"])
{
   ... // do something here
}
else
{   
   ... // image does not exist
}

The imageNamed: method will look in your main bundle for the png file.

无风消散 2024-09-12 00:45:32

只需加载资源并检查它是否为nil:

NSString* myImagePath = [[NSBundle mainBundle] pathForResource:@"MyImage" ofType:@"jpg"];
if (myImagePath != nil) {
    // Do something with image...
}

Just load the resource and check whether it is nil or not:

NSString* myImagePath = [[NSBundle mainBundle] pathForResource:@"MyImage" ofType:@"jpg"];
if (myImagePath != nil) {
    // Do something with image...
}
你的呼吸 2024-09-12 00:45:32

我会将图像分配为变量,然后您可以检查图像是否存在:

UIImage * flowerImage = [UIImage imageNamed:@"Flower.png"];

if (flowerImage) {
    // Do something
}
else {
    // Do something else
}

在接受的答案中,我们正在检查图片是否等于具有特定名称的图像。这可能会出错,因为如果

  • 图像 Flower.png 不存在

并且

  • 图像 Flower.png 确实存在,但 FlowerImage 设置为不同的图像,这意味着它们不相等,

我们将返回 no为图像分配一个变量,然后检查是否变量是否被分配将返回一个明确的“是”或“否”,具体取决于 Flower.png 是否存在

I would assign the image as a variable, then you can check if the image exists:

UIImage * flowerImage = [UIImage imageNamed:@"Flower.png"];

if (flowerImage) {
    // Do something
}
else {
    // Do something else
}

In the accepted answer we are checking to see if the picture equals an image with a specific name. This could go wrong as we will return no if

  • The image Flower.png doesn't exist

AND

  • The image Flower.png does exist but flowerImage is set to a different image meaning they are not equal

Assigning a variable to the image and then checking if the variable is assigned or not will return us a definite yes or no depending on whether Flower.png exists

爱的那么颓废 2024-09-12 00:45:32

我什至没有将“.png”添加到文件名中,它就可以工作:

NSArray *images = @[@"Flower", @"Car", @"Tree", @"Cat", @"Shoe"];

if ([UIImage imageNamed:images[0]]){
        // there is a flower image
    } else {
        // no flowers for you
    }
}

I don't even add the ".png" to the file name and it works:

NSArray *images = @[@"Flower", @"Car", @"Tree", @"Cat", @"Shoe"];

if ([UIImage imageNamed:images[0]]){
        // there is a flower image
    } else {
        // no flowers for you
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文