2015-03-13 92 views
0

ALAssetLibrary可以非常容易地從相機膠捲或相冊中獲取圖像文件信息。但是,當我嘗試使用它來加載Document文件夾(沙箱)中的圖像文件時,它始終沒有任何結果。這是我的代碼:ALAssetLibrary可以在Document文件夾中加載圖像文件嗎?

//Get full file path in the Document folder.(Yes, IMG001.jpg exists in that place) 
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString* theFileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"IMG001.jpg"]; 
NSURL* theFileURL = [NSURL fileURLWithPath:theFileName]; 

//Now try to load the asset. 
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
[library assetForURL:theFileURL resultBlock:^(ALAsset *asset) { 
    //asset here is always nil. 
} failureBlock:^(NSError *error) { 
    //it does not run in this place. 
}]; 

有人可以告訴我爲什麼嗎?

回答

0

傳遞給的資產assetForURL函數應該是之前從ALAsset對象中檢索到的資產URL。對象表示由照片應用程序管理的照片或視頻。所以在這種情況下你不能使用ALAssetLibrary。

要想從文件目錄中的圖像使用以下命令:

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString* imageFilePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"IMG001.jpg"]; 
    UIImage* imageRequired = [UIImage imageWithContentsOfFile: imageFilePath]; 
+0

感謝。但是UIImage不包含元數據。事實上,我的目的是通過元數據將我的照片保存到相機膠捲上。這可以通過ALAssetLibrary writeImageToSavedPhotosAlbum:metadata:completionBlock:的方法完成。所以,我的目標是元數據。我想通過ALAssetLibrary獲取元數據。 – guogangj 2015-03-13 07:05:52

相關問題