2016-11-16 70 views
2

我想從UIImagepickercontroller獲取文件名。我不想使用ALAssetLibrary,因爲它在iOS9中已被廢棄。請告訴如何使用它。我已經使用了下面的代碼,但它總是爲每個文件返回我的imagename作爲Asset.jpg如何使用Asset Library在uiimagepickercontroller中獲取文件名?

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {  
    let originalImage = (info[UIImagePickerControllerOriginalImage] as? UIImage)! 
    let url = info[UIImagePickerControllerReferenceURL] as! NSURL 
    imageData = UIImageJPEGRepresentation(originalImage, 100) as NSData? 
    let data = UploadData() 
    data.fileName = url.lastPathComponent  
    picker.dismiss(animated: true, completion: nil) 
    } 

回答

10

我會建議你使用Photos框架來獲取圖像的名稱,下面是代碼即可獲得所選圖像的名稱

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
    if let imageURL = info[UIImagePickerControllerReferenceURL] as? URL { 
     let result = PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil) 
     let asset = result.firstObject 
     print(asset?.value(forKey: "filename")) 

    } 

    dismiss(animated: true, completion: nil) 
} 
+2

* PHAsset.fetchAssets(withALAssetURLs:[imageURL],options:nil)*已在iOS 11中棄用。您是否有替換品? –

+3

此代碼不適用於ios 11.0.3。任何解決方案 –

1

做如下。

我相信它會幫助你。

這裏我們將選定的圖像保存到文檔目錄中,然後嘗試從文檔目錄中訪問這些圖像。

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { 
    let imageURL = info[UIImagePickerControllerReferenceURL] as NSURL 
    let imageName = imageURL.path!.lastPathComponent 
    let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String 
    let localPath = documentDirectory.stringByAppendingPathComponent(imageName) 
    let image = info[UIImagePickerControllerOriginalImage] as UIImage 
    let data = UIImagePNGRepresentation(image) 
    data.writeToFile(localPath, atomically: true) 
    let imageData = NSData(contentsOfFile: localPath)! 
    let photoURL = NSURL(fileURLWithPath: localPath) 
    let imageWithData = UIImage(data: imageData)! 
    picker.dismissViewControllerAnimated(true, completion: nil) 
} 
5

這對我的作品,而不必依賴於filename鍵(請原諒的力量展開自選:))

extension ConversationDetailViewController : UIImagePickerControllerDelegate, UINavigationControllerDelegate { 
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
     guard let image = info[UIImagePickerControllerEditedImage] as? UIImage else { 
      DDLogDebug("No image chosen") 
      return 
     } 

     let url = info[UIImagePickerControllerReferenceURL] as! URL 
     let assets = PHAsset.fetchAssets(withALAssetURLs: [url], options: nil) 
     let fileName = PHAssetResource.assetResources(for: assets.firstObject!).first!.originalFilename 

     DDLogDebug("File name = \(fileName)") 

     dismiss(animated: true) 
    } 
} 
0

導入照片

func getFileName(info: [String : Any]) -> String { 

    if let imageURL = info[UIImagePickerControllerReferenceURL] as? URL { 
     let result = PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil) 
     let asset = result.firstObject 
     let fileName = asset?.value(forKey: "filename") 
     let fileUrl = URL(string: fileName as! String) 
     if let name = fileUrl?.deletingPathExtension().lastPathComponent { 
     print(name) 
     return name 
     } 
    } 
    return "" 

} 

調用它裏面的方法 「didFinishPickingMediaWithInfo」 像這樣:

let fileName = getFileName(info: info) 
0

你可以試試這個爲iOS 11

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
    if let asset = info[UIImagePickerControllerPHAsset] as? PHAsset { 
     if let fileName = (asset.value(forKey: "filename")) as? String { 
      //Do your stuff here 
     } 
    } 

    picker.dismiss(animated: true, completion: nil) 
} 

確保你這個NSPhotoLibraryUsageDescription添加到您的Infor.plist

+0

無法使用ios 11.0.3中的代碼獲取文件名。任何解決方案 –

+0

您是否已將NSPhotoLibraryUsageDescription關鍵字和值添加到Info.plist中?然後在出現系統彈出窗口時批准設備的許可請求。 –

+0

是的。我在info.plist中給出了 –

相關問題