如何从资源网址保存视频

问题描述:

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

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"

我试过这个: -

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长度]:无法识别的选择器发送到实例

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获取任何帮助。

Thanx for any help.

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

I think your best bet is to use the method

getBytes:fromOffset:length:error:

ALAssetRepresentation

您可以获得资产的默认表示

You can get the default representation of an asset like so

ALAssetRepresentation *representation = [someVideoAsset defaultRepresentation];

所以我的头脑应该是这样的(我离开了来自我的Mac,因此未经测试)

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]);
}];

videoUrl 是您尝试的视频的资产网址复制, filePath 是您尝试将其保存到的路径。

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.