From all of the examples I've seen, I do believe I'm doing this right. Here is my code:
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0];
NSString* thisImage = [documentsPath stringByAppendingPathComponent:image_path];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:thisImage];
if (fileExists){
hotel_image = [UIImage imageNamed:image_path];
}else{
hotel_image = [UIImage imageNamed:@"hdrHotels.jpg"];
}
You can assume that "image_path" equals "images/hotels/thishotel.jpg".
Now first, so that everyone understands, "hdrHotels.jpg" is in the base directory. And "images/hotels/thishotel.jpg" are actual subdirectories (blue folers). I've also tried using:
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *imagePath = [NSString stringWithFormat:@"/%@",image_path];
NSString* thisImage = [documentsPath stringByAppendingPathComponent:imagePath];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:thisImage];
Adding a leading "/" in front of "images/hotels/thishotel.jpg". What is happening in both of these cases is that the "if" statement is false whether the image exists or not. I'm not sure what I'm doing wrong at all. Any help would be greatly appreciated.
EDIT:
I changed:
NSString *imagePath = [NSString stringWithFormat:@"/%@",image_path];
to:
NSString *imagePath = [image_path stringByReplacingOccurrencesOfString:@"images/hotels/" withString:@""];
But that still didn't work.
I also tried:
NSFileManager* fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString* documentsDir = [paths objectAtIndex:0];
NSString* myFilePath = [NSString stringWithFormat:@"%@/%@", documentsDir, image_path];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myFilePath];
with no luck.
---------------------------------- ANSWER --------------------------------
So yeah, I finally figured it out. I can't answer my own question since I don't have 100 rep yet, but here is the fix.
NSFileManager* fileManager = [NSFileManager defaultManager];
NSString *myFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:image_path];
BOOL fileExists = [fileManager fileExistsAtPath:myFilePath];
if (fileExists){
hotel_image = [UIImage imageNamed:image_path];
}else{
hotel_image = [UIImage imageNamed:@"hdrHotels.jpg"];
}
Now that I see this again I can finally answer it:
NSFileManager* fileManager = [NSFileManager defaultManager];
NSString *myFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:image_path];
BOOL fileExists = [fileManager fileExistsAtPath:myFilePath];
if (fileExists){
hotel_image = [UIImage imageNamed:image_path];
}else{
hotel_image = [UIImage imageNamed:@"hdrHotels.jpg"];
}