Note that there are some explanatory texts on larger screens.

plurals
  1. POiPhone if file exists issue
    primarykey
    data
    text
    <p>From all of the examples I've seen, I do believe I'm doing this right. Here is my code:</p> <pre><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"]; } </code></pre> <p>You can assume that "image_path" equals "images/hotels/thishotel.jpg".</p> <p>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:</p> <pre><code>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]; </code></pre> <p>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.</p> <p>EDIT:</p> <p>I changed:</p> <pre><code>NSString *imagePath = [NSString stringWithFormat:@"/%@",image_path]; </code></pre> <p>to:</p> <pre><code>NSString *imagePath = [image_path stringByReplacingOccurrencesOfString:@"images/hotels/" withString:@""]; </code></pre> <p>But that still didn't work.</p> <p>I also tried:</p> <pre><code>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]; </code></pre> <p>with no luck.</p> <blockquote> <p>---------------------------------- ANSWER --------------------------------</p> </blockquote> <p>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.</p> <pre><code>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"]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload