将图像保存到iOS文档目录

时间:2015-09-09 08:47:20

标签: ios nsdata nsfilemanager

我一直在关注SO Stack Overflow Answer

的回答

问题是文件路径不会返回应该保存到应用程序文件结构中的Documents文件夹的任何数据。我将文件路径保存到CoreData,这样可以正常工作,但是,当尝试使用文件路径访问数据时,它会返回" null"。

我将图片保存在 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];
    }

使用Managed Object子类" Image"弹出一个segue来加载一个数组。该数组位于Gallery.m中。我在-(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];

路径注销正常但在使用文件路径获取数据时失败。

文件路径注销:

  

的/ var /移动/容器/数据/应用/ E9375631-8ACD-4A0F-861B-649E711A36CF /文档/ 5FCB8C87-8B55-4246-99A3-3B933DDCEFA9

2 个答案:

答案 0 :(得分:3)

用于写入图像

 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];

用于阅读图像

 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];
 }

以上编码您的相关问题。

答案 1 :(得分:0)

之前我遇到过同样的问题。 您将图像写入路径并将其保存到核心数据。稍后读取图像路径,它完全相同但没有加载图像。

那么,您是否在模拟器中测试了应用程序?我不知道它是故意还是某种bug,但它可以在真实设备上正常工作。

因此,请尝试在真实设备中运行您的应用,看看它是否有效。