gpt4 book ai didi

objective-c - NSFileManager fileExistsAtPath 仅适用于绝对路径

转载 作者:行者123 更新时间:2023-11-29 13:26:05 25 4
gpt4 key购买 nike

我在一个文件夹中有一堆客户图片(名为 [code].png),但有些客户没有图片,所以我想显示一个占位符。

我正在检查图像 [code].png 是否存在,如果存在,我会显示该图像,否则我会显示占位符。但是,fileExistsAtPath 总是 返回 false,即使对于存在的图像也是如此。

NSFileManager *m    = [NSFileManager defaultManager];
NSString *imagePath = [NSString stringWithFormat:@"%@.png",code];
if ([m fileExistsAtPath:imagePath])
self.detailViewController.imageFrame.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",code]];
else
self.detailViewController.imageFrame.image = [UIImage imageNamed:@"customer_large.png"];

这始终显示 customer_large。

但是,如果我将 imagePath 更改为图像的绝对路径并将显示的图像保留为相对文件名,则 bool 值可以正常工作,并且显示的图像也可以工作.

这证明我正在检查的文件名确实存在,正如图像显示的那样,但我显然在 fileExistsAtPath 方面遗漏了一些东西,因为当程序证明文件存在时它返回 false。

最佳答案

由于图像存储在项目中,而不是文档目录中,所以我根本不会使用 NSFileManager。

相反,使用 [UIImage imageNamed:@"IMAGE_FILENAME"] 并查看它是否返回 nil。如果是,则使用占位符。

这是一个代码片段:

UIImage *possibleImage = [UIImage imageNamed:@"IMAGE_FILENAME"];
if (possibleImage) {
self.detailViewController.imageFrame.image = possibleImage;
} else {
self.detailViewController.imageFrame.image = [UIImage imageNamed:@"customer_large.png"];
}

关于objective-c - NSFileManager fileExistsAtPath 仅适用于绝对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13089992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com