NSDocumentDirectory文件在ios中消失 [英] NSDocumentDirectory files disappear in ios
问题描述
我想在我的文件夹中保存一个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成功= [videoData writeToFile:tempPath atomically:YES];
if(成功)
NSLog(@saved);
else
NSLog(@not saved !!!!!!!!!!!!!!);
我成功了,所以没关系,我可以很好地播放我的视频。
NSString * path_video = [dict objectForKey:@path]; //字典保存路径,在关闭应用程序之前和之后相同
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]];
在这一点上工作正常。
<但是当我关闭并重新打开应用程序,我得到的路径,我的应用程序崩溃,我有日志数据空
我不明白为什么当我关闭我的应用程序文件消失...怎么了?
$ b
谢谢
这是因为在iOS 8 +中,Application文件夹的名称在每次启动时都会重命名。在/ Users /您的用户名/ Library / Developer / CoreSimulator / Devices /设备名称/ data / Containers / Data / Application /应用程序名称中检查它/
(在模拟器中测试)。
所以,你必须保存没有文档目录的路径。而当你试图检索路径,你必须添加文件目录之前保存的路径。
就像让你的自定义文件夹名称是Save_Video和文件名称是video_01.mp4。
您的文件保存路径将为应用程序文档目录/Save_Video/video_01.mp4
应用程序文档目录/Save_Video/video_01.mp4
I want to save a mp4 video in my folder but when I open again the app, this file is nil. But when I save the file, I can open it, so it seems that it disappears from the folder.
Save:
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!!!!!!!!!!!!!!");
I get the success in true so it's ok and I can play my video well.
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]];
and at this point it work fine.
But when I close and open again the app and I get the path, my app crash and I have the log "DATA NULL
" I don't understand why when I close my app the file disappear... what's up?
thanks
This is because in iOS 8 + the name of the Application folder is renamed each time you launch it.
Check it in /Users/"your username"/Library/Developer/CoreSimulator/Devices/"device name"/data/Containers/Data/Application/"application name" (Test in simulator).
So, you have to save the path without the document directory. And when you are trying to retrieve the path you have to add the document directory before the path you saved previously.
Like let your custom folder name is "Save_Video" and file name is "video_01.mp4". Your file saving path will be "Application document directory"/Save_Video/video_01.mp4
Then you have to store only "Save_Video/video_01.mp4"(in Database/ NSUserDefaults) and when you are retrieving the file the path should be
"Application document directory"/Save_Video/video_01.mp4
这篇关于NSDocumentDirectory文件在ios中消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!