如何从资产网址保存视频 [英] How to save video from assets url

查看:9
本文介绍了如何从资产网址保存视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从资产 url 将视频保存到我的应用文档中.我的资产网址如下:-

"assets-library://asset/asset.MOV?id=1000000394&ext=MOV"

我试过这个:-

NSString *str=@"assets-library://asset/asset.MOV?id=1000000394&ext=MOV";NSData *videoData = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];[videoData writeToFile:mypath atomically:YES];

但是在第二行 [NSData dataWithContentsOfURL:[NSURL URLWithString:str]] 我得到了程序崩溃的原因:-由于未捕获的异常NSInvalidArgumentException"而终止应用,原因:'-[NSURL 长度]:无法识别的选择器已发送到实例

我想知道如何访问资产视频网址.

感谢任何帮助.

解决方案

我认为你最好的办法是使用方法

getBytes:fromOffset:length:error:

ALAssetRepresentation

你可以像这样获得资产的默认表示

ALAssetRepresentation *representation = [someVideoAsset defaultRepresentation];

所以我想它应该是这样的(我不在我的 Mac 上,所以这个还没有经过测试)

ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];[assetLibraryassetForURL:videoUrl resultBlock:^(ALAsset *asset) {ALAssetRepresentation *rep = [资产 defaultRepresentation];字节 *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];[数据 writeToFile:filePath atomically:YES];} 错误块:^(NSError *err){NSLog(@"错误: %@",[错误的本地化描述]);}];

videoUrl 是您要复制的视频的资产 url,filePath 是您要保存的路径.p>

I want to save video to my app document from asset url. My asset url is as follows:-

"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

I want to know how to access asset video url.

Thanx for any help.

解决方案

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't been tested)

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 videoUrl is the asset url of the video you're trying to copy, and filePath is the path where you're trying to save it to.

这篇关于如何从资产网址保存视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆