【问题标题】:How to get the name of file for reference url using Photos framework?如何使用照片框架获取文件名以供参考 url?
【发布时间】:2016-05-30 09:36:02
【问题描述】:

这就是我获得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 起已弃用

【问题讨论】:

  • 是的,但不能这么复杂,另外资产不是通过那里的url获取的。
  • 您仍然可以使用旧方式已弃用并不意味着它已被删除或您无法使用。否则你必须使用它的新方法和代码
  • 但是我想用苹果的新方法:)
  • 这就是我给你这个链接的原因

标签: ios swift photosframework


【解决方案1】:

最简单的答案:

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
        }
    })
}

【讨论】:

  • 'fetchAssetsWithALAssetURLs' 在 iOS11 中已弃用
猜你喜欢
  • 2015-03-07
  • 2017-03-27
  • 1970-01-01
  • 2016-12-21
  • 1970-01-01
  • 1970-01-01
  • 2016-09-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多