Images not loading if I completely close the app


#1

Forgive me if I am wrong, but I thought this chapter was explaining how to get the app to save images to the file system and then make it possible to reload those same images if you were to completely exit the application. I have the same code as the book does with the following:

[code]- (void)setImage:(UIImage *)image forKey:(NSString *)key
{
//[self.dictionary setObject:image forKey:key];

self.dictionary[key] = image;

// Create full path of image
NSString *imagePath = [self imagePathForKey:key];

// Turn image into JPEG data
NSData *data = UIImageJPEGRepresentation(image, 0.5);

// Write it to full path
[data writeToFile:imagePath atomically:YES];
NSLog(@"I think it wrote the image...");

}

  • (UIImage *)imageForKey:(NSString *)key
    {
    //return [self.dictionary objectForKey:key];
    //return self.dictionary[key];

    // If possible, get it from the dictionary
    UIImage *result = self.dictionary[key];

    if(!result)
    {
    NSString *imagePath = [self imagePathForKey:key];

      // Create UIImage from file
      result = [UIImage imageWithContentsOfFile:imagePath];
      
      //If we found an image on the file system place it into the cache
      if(result)
      {
          self.dictionary[key] = result;
          NSLog(@"Inside IF, found image for key on filesystem...");
          
      }
      else
      {
          NSLog(@"Error: Unable to find %@", [self imagePathForKey:key]);
      }
    

    }

    return result;

}[/code]

I am able to take pictures, press the home button and return to app while it is in the background state and the images are still there. However, if I try to completely close the application and reopen it, the data from the item is there (name, serial, etc), but the images are never reloaded. In fact, my NSLog I placed inside of imageForKey that says “Inside IF, found image for key on filesystem…” is never reached.

What could be causing this?

EDIT: I figured out what was causing the issue, but I am not sure why it would make a difference. The error was in the following block of code:

[code]- (NSString *)imagePathForKey:(NSString *)key
{
NSArray *documentDirectories =
NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);

NSString *documentDirectory = [documentDirectories firstObject];

return [documentDirectory stringByAppendingPathComponent:key];

}[/code]

As you can see, I accidentally was using NSDocumentationDirectory instead of NSDocumentDirectory. Why would this matter, though? As long as the images and their paths are always pointing to the same place, shouldn’t it still be able to grab the images?


#2

You’ll notice that when using NSDocumentationDirectory that the save method actually failed. If you have the log messages shown in the book, you’ll see that failure printed to the console log. The reason that it fails is because the NSDocumentationDirectory is outside of your application sandbox. As mentioned at the beginning of the chapter, the only place you are able to save things to are folders within your own application sandbox.

Since the saving fails, nothing is actually getting saved to the file system. So it makes sense then that when you launch the application the next time, there is nothing saved to actually load in.