在 iOS 中保存/检索视频文件
我一直在浏览但找不到解决我的问题的方法。我正在尝试将一些视频文件保存到我的应用程序的目录中,我按如下方式执行此操作:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"myFile"];
NSLog(@"%@",appFile);
[[NSUserDefaults standardUserDefaults] setValue:appFile forKey:@"videoURL"];
[[NSUserDefaults standardUserDefaults]synchronize];
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *webData = [NSData dataWithContentsOfURL:videoURL];
[webData writeToFile:appFile atomically:YES];
我的问题是,这是保存电影文件的正确方法吗?如果是,如何将 NSData 转换为可以播放的文件通过 MPMoviePlayer?感谢您的帮助。
附言。我真的不想使用照片库,因为该应用程序可能包含大量视频。
I've been browsing around but couldn't find a solution to my problem. I'm trying to save some video files to my application's directory I do this as follows:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"myFile"];
NSLog(@"%@",appFile);
[[NSUserDefaults standardUserDefaults] setValue:appFile forKey:@"videoURL"];
[[NSUserDefaults standardUserDefaults]synchronize];
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *webData = [NSData dataWithContentsOfURL:videoURL];
[webData writeToFile:appFile atomically:YES];
My question is, is this the proper way of saving the movie file and if yes how do I convert the NSData to a file that can be played back via MPMoviePlayer? Thanks for your help.
PS. I really don't want to use the photo library as the app is likely to hold a fair amount of videos.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您确实想将其保存在应用程序的文档中,则必须使用 NSFileManager
它可能看起来像这样,
您可能还需要使用 NSFileHandle
要在 MPMoviePlayer 中读取视频,如果您认为可以创建这样的 NSURL
,那么您将该 url 提供给您的 MPMoviePlayer。
我希望这会对您有所帮助。
If you really want to save it in the documents of your app, you'll have to use NSFileManager
it would probably look like something like that
You also might need to use NSFileHandle
To read the video in your MPMoviePlayer, if think you can make an NSURL like this
then you give that url to your MPMoviePlayer.
I hope this will help you.