xcode 如何从资产网址保存视频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6568210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save video from assets url
提问by Dhawal
I want to save video to my app document from asset url. My asset url is as follows:-
我想将视频从资产 url 保存到我的应用程序文档中。我的资产网址如下:-
"assets-library://asset/asset.MOV?id=1000000394&ext=MOV"
I tried this:-
我试过这个:-
NSString *str=@"assets-library://asset/asset.MOV?id=1000000394&ext=MOV";
NSData *videoData = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
[videoData writeToFile:mypath atomically:YES];
but on the second line [NSData dataWithContentsOfURL:[NSURL URLWithString:str]] i got program crash with this reason:- Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL length]: unrecognized selector sent to instance
但是在第二行 [NSData dataWithContentsOfURL:[NSURL URLWithString:str]] 上,我的程序崩溃了,原因是:- 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSURL 长度]:无法识别的选择器发送到实例
I want to know how to access asset video url.
我想知道如何访问资产视频网址。
Thanx for any help.
感谢任何帮助。
回答by Mic Pringle
I think your best bet is to use the method
我认为你最好的选择是使用该方法
getBytes:fromOffset:length:error:
of
的
ALAssetRepresentation
You can get the default representation of an asset like so
您可以像这样获得资产的默认表示
ALAssetRepresentation *representation = [someVideoAsset defaultRepresentation];
So off the top of my head it should go something like this (I'm away from my Mac so this hasn'tbeen tested)
所以在我的脑海里它应该是这样的(我远离我的 Mac 所以这还没有经过测试)
ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
[assetLibrary assetForURL:videoUrl resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *rep = [asset defaultRepresentation];
Byte *buffer = (Byte*)malloc(rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
[data writeToFile:filePath atomically:YES];
} errorBlock:^(NSError *err) {
NSLog(@"Error: %@",[err localizedDescription]);
}];
Where videoUrlis the asset url of the video you're trying to copy, and filePathis the path where you're trying to save it to.
其中videoUrl是您尝试复制的视频的资产 url,filePath是您尝试将其保存到的路径。
回答by mrburns05
thanks for this.. all i needed to change was
谢谢你……我需要改变的是
errorBlock:^(NSError *err)
to this:
对此:
failureBlock :^(NSError *err)