How to save video from assets url

僤鯓⒐⒋嵵緔 提交于 2019-11-28 22:35:29

问题


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.


回答1:


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.




回答2:


thanks for this.. all i needed to change was

    errorBlock:^(NSError *err)

to this:

    failureBlock :^(NSError *err)


来源:https://stackoverflow.com/questions/6568210/how-to-save-video-from-assets-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!