UIImage 保存到文档目录返回零

UIImage saved to Document Directory returning nil

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

-我检查文件是否存在于路径中,确实存在,但是当我尝试 NSLog 数据时,returns 为空。

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

这是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;
}

您有几个问题:

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

您的文件可能无法在加载时找到,因为您的存储路径包含可能因构建而异的部分。查看此问题以获取类似的 issue/result。

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