George
George

Reputation: 534

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

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...

Upvotes: 0

Views: 94

Answers (1)

rmaddy
rmaddy

Reputation: 318774

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.

Upvotes: 1

Related Questions