2010-11-30 56 views
23

我在iPhone應用程序中從照片庫中選擇圖像。我將如何檢索實際的圖像名稱。我怎樣才能得到通過iPhone中的照片庫採摘的圖像的名稱?

在.H類

UIImageView * imageView; 

UIButton * choosePhotoBtn; 

中的m級

-(IBAction) getPhoto:(id) sender 
{ 
    UIImagePickerController * picker = [[UIImagePickerController alloc] init]; 
    picker.delegate = self; 
    if((UIButton *) sender == choosePhotoBtn) 
    { 
     picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
    } 
    else 
    { 
     picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
    } 
    [self presentModalViewController:picker animated:YES]; 
} 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    [picker dismissModalViewControllerAnimated:YES]; 
    imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
} 

我如何獲得圖像的實際名稱?

我新的iphone。請幫幫我。

在此先感謝。

+0

看到此鏈接 http://stackoverflow.com/questions/5864814/uiimagepickercontroller-get-the-name-of-the -image-selected-from-photo-library – PRASAD1240 2013-03-29 09:08:33

+0

http://stackoverflow.com/questions/5864814/uiimagepickercontroller-get-the-name-of-the-image-selected-from-photo-library – PRASAD1240 2013-03-29 09:47:01

+0

@ PRASAD1240你爲什麼要發佈問題後面提問的鏈接?請至少檢查問題日期。 – 2016-10-19 08:01:39

回答

67

進口AssetsLibrary:

#import <AssetsLibrary/AssetsLibrary.h> 

而且,在- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

// get the ref url 
NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL]; 

// define the block to call when we get the asset based on the url (below) 
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset) 
{ 
    ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation]; 
    NSLog(@"[imageRep filename] : %@", [imageRep filename]); 
}; 

// get the asset library and fetch the asset based on the ref url (pass in block above) 
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init]; 
[assetslibrary assetForURL:refURL resultBlock:resultblock failureBlock:nil]; 

所以你會得到在日誌中的映像名稱。

*不要忘記添加現有的框架:AssetsLibrary。框架 步驟:

  1. 在項目導航,選擇項目
  2. 選擇目標
  3. 選擇「構建階段」選項卡
  4. 打開「鏈接二進制與圖書館」擴展
  5. 點擊'+'按鈕
  6. 選擇您的框架
  7. (可選)將添加的框架拖放到「框架」組中

來源: http://www.raywenderlich.com/forums/viewtopic.php?f=2&p=34901 & How to "add existing frameworks" in Xcode 4?

+2

這在我看來是最好的答案。 – 2013-05-16 20:36:30

+1

比接受的答案更好,但即使對於Safari下載的圖像,圖像名稱仍然類似於「IMG_1754.JPG」。但是,這看起來是這樣的,Image Capture顯示了相同的名稱。 – 2014-05-03 20:57:22

+1

iOS 9中的棄用警告。Equiavlent是fetchAssestsWithLocalIdentifiers:選項 – 2015-10-14 09:21:37

2

雖然您可能能夠檢索最後一個路徑組件並將其用作文件名,但建議您不要這樣做。這些文件名由系統分配給iTunes,以便在同步時理解,並且不適合程序員訪問,因爲他們可能會在未來的同步中被其他圖像替換。

對此的一個好處是將當前日期指定爲文件名,同時保存到從庫中選取的圖像。您可以將其保存在文檔或庫目錄中,並使用映射PList文件將圖像映射到其文件名。

或者,您也可以指定唯一的數字作爲文件名並使用這些值訪問圖像。在文件

4

簡單迅速實施:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 

    if let referenceUrl = info[UIImagePickerControllerReferenceURL] as? NSURL { 

     ALAssetsLibrary().assetForURL(referenceUrl, resultBlock: { asset in 

      let fileName = asset.defaultRepresentation().filename() 
      //do whatever with your file name 

      }, failureBlock: nil) 
     } 
    } 
} 

記住:import AssetsLibrary

7

如果您正在爲iOS的9+目標,你會看到使用ALAssetsLibrary的一堆棄用警告,即:

'assetForURL(_:resultBlock:failureBlock :)' 被棄用的iOS 9.0:使用fetchAssetsWithLocalIdentifiers:選擇:對PHAsset本地標識符(獲取資產或由先前已知的ALAssetPropertyAssetURL使用fetchAssetsWithALAssetURLs查找PHAssets :options :)而不是

正如警告所述,您應該使用PHAsset。例如,使用swift 2.x,您需要先將import Photos添加到您的文件中。然後,在didFinishPickingMediaWithInfo UIImagePickerControllerDelegate方法使用fetchAssetsWithALAssetURLs來獲取文件名:

if let imageURL = info[UIImagePickerControllerReferenceURL] as? NSURL { 
    let result = PHAsset.fetchAssetsWithALAssetURLs([imageURL], options: nil) 
    let filename = result.firstObject?.filename ?? "" 
} 

這將設置filename是類似「IMG_0007.JPG」。

3

在Objective C,使用照片框架,並導入照片/ Photos.h

然後,在你imagePickerController功能 添加以下內容從照片庫中獲取圖像的文件名

NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL]; 
PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[refURL] options:nil]; 
NSString *filename = [[result firstObject] filename]; 
0

Objective C實施方案iOS 10。ALAssetsLibrary似乎被棄用,所以你應該使用PHAsset:

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ 

    NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL]; 
    PHAsset *phAsset = [[PHAsset fetchAssetsWithALAssetURLs:@[imageURL] options:nil] lastObject]; 
    NSString *imageName = [phAsset valueForKey:@"filename"]; 

    UIImage *photo = [info valueForKey:UIImagePickerControllerOriginalImage]; 

    NSLog(@"Picked image: %@ width: %f x height: %f",imageName, photo.size.width, photo.size.height); 

    [picker dismissViewControllerAnimated:YES completion:nil]; 
} 
0
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) 
{ 
    let imageUrl   = info[UIImagePickerControllerReferenceURL] as! NSURL 
    let imageName   = imageUrl.lastPathComponent 
    let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! 
    let photoURL   = NSURL(fileURLWithPath: documentDirectory) 
    let localPath   = photoURL.appendingPathComponent(imageName!) 
    let image    = info[UIImagePickerControllerOriginalImage]as! UIImage 
    let data    = UIImagePNGRepresentation(image) 

    do 
    { 
     try data?.write(to: localPath!, options: Data.WritingOptions.atomic) 
    } 
    catch 
    { 
     // Catch exception here and act accordingly 
    } 

    self.dismiss(animated: true, completion: nil); 
} 
相關問題