UIImage保存到文档目录,返回nil

时间:2015-01-24 18:11:15

标签: ios objective-c nsdata nsfilemanager nsdocumentdirectory

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

-I检查文件是否存在于路径中,但是当我尝试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];
}

这是用于检索Collection View

的存储数据的代码
- (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;
}

2 个答案:

答案 0 :(得分:3)

您有几个问题:

  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 /文档/ 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);
    }
    

答案 1 :(得分:1)

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

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

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

相关问题