2015-09-04 51 views
1

我想獲取圖庫圖像文件路徑並在關閉此視圖後保存此文件路徑並打開另一個視圖使用此文件路徑引用顯示此圖像...如何使用文件路徑引用獲取圖片庫文件路徑和顯示圖像

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
NSLog(@"%@",info); 

} 

使用該委託如何讓映像文件的路徑,並顯示此圖像的另一個觀點

+1

check(http://stackoverflow.com/questions/7777282/how-to-get-image-path-from-photo-library-and-how-to-retrieve-the-image-from-phot )。它會顯示如何獲取所選圖像的URL。 –

+0

感謝您的回覆解決我的問題,但我想打開相機,然後單擊(使用此圖像)選項後點擊圖像沒有得到圖像路徑不顯示圖像如何解決這個 –

+0

因爲你應該先保存圖像使用'ALAssetLibrary'然後只有你會得到點擊圖像和路徑。 –

回答

2

在映像文件的路徑,你會得到資產的網址,並且可以使用該資產URL來顯示圖像在圖像視圖上。

首次進口

#import <AssetsLibrary/AssetsLibrary.h> 

在你的項目,然後在UIImagePicker

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    // You will get the image url like this 
    NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL]; 
} 

委託方法現在來顯示圖像,使用此代碼

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) { 
     ALAssetRepresentation *rep = [myasset defaultRepresentation]; 
     CGImageRef iref = [rep fullResolutionImage]; 
     if (iref) { 
      //here you get the image 
      largeimage = [UIImage imageWithCGImage:iref]; 
     } 
    }; 

ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init]; 
[assetslibrary assetForURL:imageURL 
        resultBlock:resultblock 
        failureBlock:nil]; 

如果直接想要訪問該圖像,然後使用這個

UIImage* myImage=(UIImage*)[info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
+0

感謝Rajatp爲您的迴應上面的代碼解決了我的問題,但我想打開相機,並單擊(使用此圖像)後單擊圖像沒有得到圖像路徑如何解決這 –

+0

如果你不會從相機獲得圖像路徑希望您必須將該圖像保存到圖庫中,然後才能從資產中獲取路徑。 – Rajat

+0

要做到這一點檢查此鏈接 - http://stackoverflow.com/questions/4457904/iphone-how-do-i-get-the-file-path-of-an-image-saved-with-uiimagewritetosavedpho – Rajat

相關問題