iOS8 Photos Framework: How to get the name(or filename) of a PHAsset?

Priyanka picture Priyanka · Jan 9, 2015 · Viewed 32.9k times · Source

Im trying to get the image name using PHAssets. But I couldn't find metadata for filename or any method to get the image name. Is there a different way to get the file name?

Answer

skim picture skim · Oct 29, 2015

I know the question has already been answered, but I figured I would provide another option:

extension PHAsset {

    var originalFilename: String? {

        var fname:String?

        if #available(iOS 9.0, *) {
            let resources = PHAssetResource.assetResources(for: self)
            if let resource = resources.first {
                fname = resource.originalFilename
            }
        }

        if fname == nil {
            // this is an undocumented workaround that works as of iOS 9.1
            fname = self.value(forKey: "filename") as? String
        }

        return fname
    }
}