这就是我获取 NSURL
的方式:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let referenceUrl = info[UIImagePickerControllerReferenceURL] as? NSURL {
//access the file name
}
}
它正在使用 ALAssetsLibrary
:
let assetsLibrary = ALAssetsLibrary()
assetsLibrary.assetForURL(referenceUrl, resultBlock: { asset in
let filename = asset.defaultRepresentation().filename()
}, failureBlock: nil)
但自 iOS 9 起它已被弃用。
最佳答案
最简单的答案:
if let asset = PHAsset.fetchAssetsWithALAssetURLs([referenceUrl], options: nil).firstObject as? PHAsset {
PHImageManager.defaultManager().requestImageDataForAsset(asset, options: nil, resultHandler: { _, _, _, info in
if let fileName = (info?["PHImageFileURLKey"] as? NSURL)?.lastPathComponent {
//do sth with file name
}
})
}
关于ios - 如何使用照片框架获取引用网址的文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37522327/