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?