How to tell if a file exists always returns false but I can load it via image tag

匿名 (未验证) 提交于 2019-12-03 01:46:01

问题:

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"; } 

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.

Geo...

回答1:

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.

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.

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!