简体   繁体   中英

Saving Image to iOS documents directory

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

For WRITE the image

 NSData *data = [NSData dataWithBase64EncodedString:strImageURL];
 UIImage *image = [UIImage imageWithData:data];
 NSData *pngData = UIImagePNGRepresentation(image);
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",textFieldName.text]];
 [pngData writeToFile:getImagePath atomically:YES];

For READ the image

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",strName]];
 UIImage *image = [UIImage imageWithContentsOfFile:getImagePath];
 if(image != nil)
 {
   imageView.image = [UIImage imageWithContentsOfFile:getImagePath];
 }

Above coding for your relevant question.

I was encountered the same problem before. You write a image to a path and save it to the core data. Later read the image path, it is exactly same but no image is loaded.

So, Did you tested the app in the simulator? I don't know if it is intended or a kind of a bug but it would be working as you wanted on the real devices.

So try to run your app in the real devices and see if it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM