我想在我的文件夹中保存一个 mp4 视频,但是当我再次打开应用程序时,这个文件为零。但是当我保存文件时,我可以打开它,所以它似乎从文件夹中消失了。
保存:
NSData *videoData = [NSData dataWithContentsOfURL:exportUrl];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *tempPath = [documentsDirectory stringByAppendingFormat:@"/%@",videoName];
self.path_video_to_save = tempPath;
BOOL success = [videoData writeToFile:tempPath atomically:YES];
if (success)
NSLog(@"saved");
else
NSLog(@"not saved!!!!!!!!!!!!!!");
我获得了真正的成功,所以没关系,我可以很好地播放我的视频。
NSString *path_video = [dict objectForKey:@"path"]; //dictionary where I save the path, the same before and after closing app
NSData *videoData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:path_video]];
if (videoData == nil){
NSLog(@"DATA NULL");
}
else
NSLog(@"DATA OK");
NSLog(@"PATH:%@", path_video);
self.player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path_video]];
此时它工作正常。
但是当我关闭并再次打开该应用程序并获得路径时,我的应用程序崩溃并且我有日志“DATA NULL
”我不明白为什么当我关闭我的应用程序时文件消失……怎么了?
谢谢
最佳答案
这是因为在 iOS 8 + 中,应用程序文件夹的名称在您每次启动时都会重命名。
在/Users/"your username"/Library/Developer/CoreSimulator/Devices/"device name"/data/Containers/Data/Application/"application name"中检查(在模拟器中测试)。
因此,您必须保存没有文档目录的路径。当您尝试检索路径时,您必须在之前保存的路径之前添加文档目录。
比如让您的自定义文件夹名为“Save_Video”,文件名为“video_01.mp4”。 您的文件保存路径为“申请文件目录”/Save_Video/video_01.mp4
然后你只需要存储“Save_Video/video_01.mp4”(在数据库/NSUserDefaults 中),当你检索文件时,路径应该是
《申请文件目录》/Save_Video/video_01.mp4
关于ios - NSDocumentDirectory 文件在 ios 中消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31452098/