I've been following this answer from SO Stack Overflow Answer
Problem is the file path does not return any data that should be saved to Documents folder in the app file structure. I save the file path to CoreData
, and this works fine, however, when trying to access the data with the file path, it returns "null".
I save the images in Preview.m
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
NSManagedObjectContext *context = delegate.managedObjectContext;
NSManagedObject *photoAndDate = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:context];
[photoAndDate setValue:[NSDate date] forKey:@"date"];
//unique filename
NSString *uuidString = [[NSUUID UUID] UUIDString];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:uuidString];
//set Core Data Value
[photoAndDate setValue:filePath forKey:@"image"];
//create data from image at path
NSData *pngData = UIImagePNGRepresentation(self.previewImage.image);
[pngData writeToFile:filePath atomically:YES];
//save to Core Data
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
else{
[self performSegueWithIdentifier:@"unwindToMain" sender:self];
}
Pop a segue to load an array with the Managed Object subclass "Image". The array is in Gallery.m. I do this in -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
Image *image = self.array[indexPath.item];
NSString *pathForImageString = image.image;
NSData *imageData = [NSData dataWithContentsOfFile:pathForImageString];
NSLog(@"finalImage Data exists %@", imageData);
UIImage *finalImage = [UIImage imageWithData:imageData];
UIImageView *imageView = [[UIImageView alloc]initWithImage:finalImage];
imageView.frame = cell.contentView.frame;
[cell.contentView addSubview:imageView];
The path logs out fine but fails when grab the data with the file path.
File path logs out:
/var/mobile/Containers/Data/Application/E9375631-8ACD-4A0F-861B-649E711A36CF/Documents/5FCB8C87-8B55-4246-99A3-3B933DDCEFA9