【问题标题】:UIImage saved to Document Directory returning nilUIImage 保存到文档目录返回 nil
【发布时间】:2015-03-23 14:19:20
【问题描述】:

NSDocumentDirectory我是 iOS 新手。我试图构建一个简单的应用程序来更好地掌握核心数据和文件系统。我无法检索显然已存储的数据。

-我检查文件是否存在于路径中,它确实存在,但是当我尝试 NSLog 数据并返回 null 时。

非常感谢任何帮助。我的直觉是,这是一个范围界定或记忆保留问题,但我无法弄清楚。

这是 UIImagePicker 的保存方法

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *imageToAdd = info[UIImagePickerControllerOriginalImage];
    NSData *pngsData = UIImagePNGRepresentation(imageToAdd);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docPath = [paths objectAtIndex:0];
    NSString *filePath = [docPath stringByAppendingFormat:@"%@.png",[self getdateAndTime]];
    NSError *error;
    BOOL succcess = [pngsData writeToFile:filePath options:NSDataWritingAtomic error:&error];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        NSLog(@"The file exists");
    }

    if (error !=nil) {
        NSLog(@"errorIS %@", error);
    }

    CoreDataStack *cds = [CoreDataStack defaultStack];
    Photo *photo = [NSEntityDescription insertNewObjectForEntityForName:@"Photo" inManagedObjectContext:cds.managedObjectContext];
    photo.photoName = filePath;
    [cds saveContext];

    [self dismissViewControllerAnimated:YES completion:nil];
    [self.collectionView reloadData];
}

这是在集合视图中检索存储数据的代码

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PhotoCollectionViewCell *photoCell = (PhotoCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"photoCell" forIndexPath:indexPath];
    Photo *photo = [self.fetchedResultsController objectAtIndexPath:indexPath];
    NSLog(@"%@",photo.photoName);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *imageLocation = [documentsDirectory stringByAppendingString:photo.photoName];
    NSData *data = [NSData dataWithContentsOfFile:imageLocation];
    UIImage *image = [UIImage imageWithData:self.testImageData];
    NSLog(@"%@", data);

    photoCell.cellImage.image = image;
    photoCell.layer.borderColor = [[UIColor blackColor] CGColor];
    photoCell.layer.borderWidth = 2.0;

    return photoCell;
}

【问题讨论】:

  • 仅供参考 - 您错误地使用了 Documentation 目录而不是 Documents 目录。
  • 哇.. 谢谢你的收获。我修复了它,问题仍然存在
  • 不要检查error 是否为!nil,检查返回变量(success) 是否为nil
  • 注意:OP 对路径的简单 NSLog 会显示问题。

标签: ios objective-c nsdata nsfilemanager nsdocumentdirectory


【解决方案1】:

您有几个问题:

  1. NSDocumentationDirectory 替换为NSDocumentDirectory
  2. 您正在错误地构建路径。这个:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    NSString *docPath = [paths objectAtIndex:0];
    NSString *filePath = [docPath stringByAppendingFormat:@"%@.png",[self getdateAndTime]];
    

    应该是:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docPath = [paths objectAtIndex:0];
    NSString *filename = [NSString stringWithFormat:@"%.png", [self getdataAndTime]];
    NSString *filePath = [docPath stringByAppendingPathComponent:filename];
    

    注意stringByAppendingPathComponent: 的使用。这个很重要。没有它,你最终会得到:

    .../sandboxpath/Documentsfilename.png

    代替:

    .../sandboxpath/Documents/filename.png

    在这两种方法中进行相同的修复。

  3. 您正在将完整路径保存到您的 Photo 对象。永远不要保存绝对路径。它们会随着时间而改变。仅保存相对于Documents 的文件名。所以改变:

    photo.photoName = filePath;
    

    到:

    photo.photoName = filename;
    

    当您将来读取该值时,将其附加到文档路径。

  4. 您的错误检查应该是:

    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        NSLog(@"The file exists");
    } else {
        NSLog(@"errorIS %@", error);
    }
    

【讨论】:

  • 感谢您提供详细的解释帮助。非常感谢。
【解决方案2】:

加载时可能找不到您的文件,因为您存储的路径包含可以在构建之间更改的部分。查看此问题以获取类似的问题/结果。

Can't find saved file (in device) after restarting the app

最佳答案:不要存储可变部分;后备答案:确保用当前值替换可变部分。

【讨论】:

  • 这实际上是我要解决的下一个问题,但 rmaddy 的修复也解决了这个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-31
  • 2022-01-14
  • 1970-01-01
相关资源
最近更新 更多