【问题标题】:Saving Image to iOS documents directory将图像保存到 iOS 文档目录
【发布时间】:2015-12-05 03:38:49
【问题描述】:

我一直在关注 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];
    }

弹出一个 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/mobile/Containers/Data/Application/E9375631-8ACD-4A0F-861B-649E711A36CF/Documents/5FCB8C87-8B55-4246-99A3-3B933DDCEFA9

【问题讨论】:

  • 您可能需要一些时间来更改您的问题并使其变得简单,以便其他人可以快速看到真正的问题。
  • 有什么问题? 究竟是什么失败了?
  • 从 Core Data 记录时文件路径似乎没问题。尝试使用应用程序中 Documents 目录中的文件路径访问图像时,返回的数据为空。用建议更新了上面的问题。

标签: ios nsdata nsfilemanager


【解决方案1】:

为了写图片

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

以上为您的相关问题编码。

【讨论】:

    【解决方案2】:

    我之前也遇到过同样的问题。 您将图像写入路径并将其保存到核心数据。后来读取图片路径,一模一样但是没有加载图片。

    那么,您是否在模拟器中测试了该应用程序?我不知道这是有意的还是某种错误,但它会在真实设备上按您的意愿工作。

    所以尝试在真实设备上运行您的应用,看看它是否有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多