如何判断文件是否存在总是返回 false 但我可以通过图像标签加载它 [英] How to tell if a file exists always returns false but I can load it via image tag

查看:23
本文介绍了如何判断文件是否存在总是返回 false 但我可以通过图像标签加载它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 xcode 中,我创建了一个名为 IMAGES 的文件夹,并将我将使用的所有图像复制到该文件夹​​中.

In xcode, I have create a folder called IMAGES and copied all of the images I will be using into that folder.

我正在使用以下代码更新表格以插入图像:

I am updating a table using the following code to insert the image:

   NSString *picName = [NSString stringWithFormat:@"%@.jpg", [[theemplyees objectAtIndex:indexPath.row] valueForKey:@"EmpNo"]];

   cell.empPicture.image = [UIImage imageNamed:picName];

注意不需要提供位置.我想要做的是,如果用户图片不存在于直接,则将图像设置为默认图像.如:

Note the no location needing supplied. What I want to do is, if a users picture does not exist in the directly, then set the image to a default image. Such as:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

//Set image
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:picName];

//Check if the image exists at a given path
BOOL imageExists = [[NSFileManager defaultManager] fileExistsAtPath:imagePath];

//If the image doesn't exists download it.
if (!imageExists)
{
    picName = @"nopicture.jpg";
}

问题是,使用上面的方法,它总是说文件不存在.没有任何!咕噜噜那么我是否将文件放在错误的位置?我试图创建一个名为 Resources 的目录并将它们放在那里,但仍然没有工作.

The problem is, using the above, it ALWAYS says the files don't exist. NONE! Grrr So am i placing the files in the wrong location? I attempted to create a directory called Resources and place them there but still didn;t work.

非常感谢任何帮助.在此先感谢您提供的任何帮助.

Any help is greatly appreciated. Thanks in advance for any and all help.

地理位置...

推荐答案

应用程序的 Documents 目录中不会有任何内容,除非您在运行时在该目录中存储了一些内容.Documents 目录和应用程序的资源包是两个完全不同的东西.

Nothing will be in the app's Documents directory unless you store something there during runtime. The Documents directory and the app's resource bundle are two completely different things.

如果您在 Xcode 中为图像创建了一个文件夹,这些图像仍存储在应用的资源包中,而不是 Documents 文件夹中.

If you created a folder in Xcode for your images, those images are still stored in the app's resource bundle, not the Documents folder.

UIImage imageNamed: 方法只在资源包中查找.

The UIImage imageNamed: method only looks in the resource bundle.

这篇关于如何判断文件是否存在总是返回 false 但我可以通过图像标签加载它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆