简体   繁体   中英

How to get the name of file for reference url using Photos framework?

This is how I get an NSURL :

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

    if let referenceUrl = info[UIImagePickerControllerReferenceURL] as? NSURL {
        //access the file name
    }
}

It was using ALAssetsLibrary :

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

    let filename = asset.defaultRepresentation().filename()

}, failureBlock: nil)

but since iOS 9 it is deprecated .

The simplest answer:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM