2011-07-04 22 views
1

我想從資產網址將視頻保存到我的應用文檔。我的資產網址如下: -如何從資產中保存視頻url

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

我嘗試這樣做: -

NSString *[email protected]"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長]:無法識別的選擇發送到實例

我想知道如何訪問資產視頻網址。

Thanx的任何幫助。

回答

16

我認爲最好的方法是使用方法

getBytes:fromOffset:length:error: 

ALAssetRepresentation 

你可以得到一個資產的默認表示,像這樣

ALAssetRepresentation *representation = [someVideoAsset defaultRepresentation]; 

所以關閉我的頭頂應該去這樣的事情(我離開我的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]); 
}]; 

videoUrl是你想複製視頻的資源網址,並文件路徑就是你想將它保存到路徑。

+1

GR8和感謝名單micpringle這是完全正常工作 – Dhawal

2

感謝這個..所有我需要改變的是

errorBlock:^(NSError *err) 

這樣:

failureBlock :^(NSError *err) 
相關問題