【发布时间】: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