Using PHAsset

From PeformIQ Upgrade
Revision as of 23:31, 15 February 2019 by PeterHarding (talk | contribs)
Jump to navigation Jump to search
 let fetchResult = PHAsset.fetchAssets(with: .image, options: nil)
    if fetchResult.count > 0 {
        if let asset = fetchResult.firstObject {
            let date = asset.creationDate ?? Date()
            print("Creation date: \(date)")
            PHImageManager.default().requestImageData(for: asset, options: PHImageRequestOptions(),
                resultHandler: { (imagedata, dataUTI, orientation, info) in
                    if let info = info {
                        if info.keys.contains(NSString(string: "PHImageFileURLKey")) {
                            if let path = info[NSString(string: "PHImageFileURLKey")] as? NSURL {
                                print(path)
                            }
                        }
                    }
            })
        }
    }
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
    }
}
extension PHAsset {
    var originalFilename: String? {
        return PHAssetResource.assetResources(for: self).first?.originalFilename
    }
}