【发布时间】:2015-09-12 21:33:12
【问题描述】:
我的应用程序旨在从用户视频中制作缩略图,并将这些缩略图保存在应用程序文档目录中以供以后在 UICollectionView 中使用。但是,似乎图像只是临时保存的,因为重新启动应用程序后无法访问它们。我做错了什么?
我正在使用许多以前的 SO 帖子中的代码,例如这个:Storing images locally on an iOS device,所以我不确定我引入了什么错误。
我正在保存从用户录制的视频中截取的缩略图,如下所示:
NSArray *paths2 = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSURL *documentsDirectory = [paths2 lastObject];//[self documentsDirectoryURL];
NSString *prefixString = @"IdlingVideo";
NSString *guid = [[NSProcessInfo processInfo] globallyUniqueString] ;
NSString *uniqueFileName = [NSString stringWithFormat:@"%@_%@.png", prefixString, guid];
NSURL *saveLocation = [documentsDirectory URLByAppendingPathComponent:uniqueFileName];
NSString *imagePath =saveLocation.path;
[UIImagePNGRepresentation(newImage) writeToURL:saveLocation atomically:YES];
NSURL *filePathCompressed = [[NSURL alloc] initFileURLWithPath: imagePath];
我也试过这个:
NSArray *paths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths2 objectAtIndex:0];
在这两种情况下,我运行应用程序时一切正常。缩略图似乎已保存并显示在我的UICollectionView 中。在创建UICollectionViewCell 时,我还会记录该文件是否存在的确认信息:
if ([fileManager fileExistsAtPath:self.complaint.thumbnailURL.path]){
NSLog(@"file exists at path %@", self.complaint.thumbnailURL.path);
}else{
NSLog(@"file DOES NOT EXIST at path %@", self.complaint.thumbnailURL.path);
}
根据需要,当我在用户拍照后立即查看UICollectionView 时,会输出“文件存在”。因此,在一个会话中一切都很好。但是,当用户重新启动应用程序时,图像会丢失。当应用程序重新启动时,文件似乎丢失了。 UICollectionViewCell 中没有图像出现,并且记录器指示该文件不存在。
我做错了什么?
【问题讨论】:
-
如何存储图片的路径?
-
尝试打印两个路径,用于存储图像的路径和用于检索图像的路径并检查它们是否相同?
-
我相信,这些文件仍然存在,但您尝试使用错误的 URL 获取它。你能告诉我们什么是
self.complaint,你如何设置它的thumbnailURL属性 -
你应该检查Document目录的内容,可能是文件在那里
-
重新启动后,您是使用相对路径还是绝对路径来访问文件?我不认为绝对路径是稳定的。
标签: ios objective-c