1

My app is designed to make thumbnails from user videos and save these locally with in the app document directory for later use in a UICollectionView. However, it seems as though the images are saved only temporarily as they cannot be accessed when the app has been restarted. What am I doing wrong?

I am using code from many prior SO posts, such as this one: Storing images locally on an iOS device so I am not sure what error I have introduced.

I am saving a thumbnail taken from a video recorded by the user like so:

  NSArray *paths2 = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
    NSURL *documentsDirectory = [paths2 lastObject];//[self documentsDirectoryURL];
    NSString *prefixString = @"IdlingVideo";
    NSString *guid = [[NSProcessInfo processInfo] globallyUniqueString] ;
    NSString *uniqueFileName = [NSString stringWithFormat:@"%@_%@.png", prefixString, guid];        
    NSURL *saveLocation = [documentsDirectory URLByAppendingPathComponent:uniqueFileName];
    NSString *imagePath =saveLocation.path;
    [UIImagePNGRepresentation(newImage) writeToURL:saveLocation atomically:YES];
   NSURL *filePathCompressed = [[NSURL alloc] initFileURLWithPath: imagePath];

I have also tried this:

   NSArray *paths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths2 objectAtIndex:0];

In both cases, everything works fine while I am running the app. The thumbnail seems to be saved and is presented in my UICollectionView. I also log confirmation that the file exists like so when creating the UICollectionViewCell:

if ([fileManager fileExistsAtPath:self.complaint.thumbnailURL.path]){
    NSLog(@"file exists at path %@", self.complaint.thumbnailURL.path);
}else{
    NSLog(@"file DOES NOT EXIST at path %@", self.complaint.thumbnailURL.path);
}

As desired, this outputs "file exists" when I am looking at the UICollectionView right after the user has taken a picture. So everything works great within a single session. However, the image is lost when the user starts the app anew. When the app is restarted, the file appears to be lost. No image appears in the UICollectionViewCell and the logger indicates that the file does not exist.

What am I doing wrong?

Community
  • 1
  • 1
Cauchy Kun
  • 177
  • 2
  • 11
  • how do you store your image's path? – dopcn Jun 26 '15 at 03:55
  • Try printing both the paths, the one used to store the image and the one used to retrieve the image and check if they are same? – dispatchMain Jun 26 '15 at 03:57
  • I believe, that files still exist, but you're trying to get it with wrong URL. Can you, please, show us, what is `self.complaint` and how does you set its `thumbnailURL` property – Sergii Martynenko Jr Jun 26 '15 at 04:06
  • You should check the contents of Document directory, may be the file is there – Vinay Jain Jun 26 '15 at 04:53
  • Are you using a relative or absolute path to access the file once you have relaunched? I do not think the absolute path is stable. – sunny Jun 26 '15 at 13:43

1 Answers1

0

As per Apple's documentation , the contents of theDocuments directory will persist until they are explicitly deleted by the application.

Below code is the code that I have been using till now to get path to documents directory. So, it seems that you are accessing the documents directory correctly.

+ (NSString *) applicationDocumentsDirectory 
{    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}

There is very high possibility that somewhere in your code you are explicitly deleting contents of document directory.

  1. Press Cmd + Shift + F . Perform a global search in the project with search text 'removeItemAtPath:'. This is the method which is used with NSFileManager to delete a file at path specified.
  2. In case you find 'removeItemAtPath: search text in the project, add a breakpoint on that line.
  3. Run the app and see, if your application hits that breakpoint when your app comes to foreground. If yes, there is very high probability that this is the main cause of the issue you are facing. You can follow up the function call trace to find out from where it is called.

or Alternatively

  1. Find out the location of documents directory on your machine and run your app in the simulator.
  2. Check if the documents directory has contents when appDidFinishLaunching method is called by adding a breakpoint.
  3. If yes, keep stepping over using Xcode debugger to find out at which point the contents get deleted.
tek3
  • 2,095
  • 2
  • 20
  • 50