PHImageResultIsDegradedKey/PHImageFileURLKey is not found

…衆ロ難τιáo~ 提交于 2019-12-03 14:47:43

You have to use this function from PHAsset object: - (PHContentEditingInputRequestID)requestContentEditingInputWithOptions:(nullable PHContentEditingInputRequestOptions *)options completionHandler:(void (^)(PHContentEditingInput *__nullable contentEditingInput, NSDictionary *info))completionHandler;

Then you can retrieve URL this way: NSURL *url = contentEditingInput.fullSizeImageURL;

Some example Objective-C code for anyone else looking for it:

PHContentEditingInputRequestOptions *editOptions = [[PHContentEditingInputRequestOptions alloc] init];

[myPHAsset requestContentEditingInputWithOptions:editOptions completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {

    if (contentEditingInput.fullSizeImageURL) {
        //do something with contentEditingInput.fullSizeImageURL
    }

}];

In case someone is looking to achieve the same on Xamarin.iOS, this is my C# version of the swift code:

phAsset.RequestContentEditingInput(new PHContentEditingInputRequestOptions(), phcontentediting);

and then:

private void phcontentediting(PHContentEditingInput contentEditingInput, NSDictionary requestStatusInfo)
        {

        }

when phcontentediting is called, contentEditingInput has a lot of useful information such as CreationDate, FullSizeImageUrl and even Location!

1.PHImageResultIsDegradedKey is now being available in latest iOS 13 GM and GM2. So one problem solved.

2.PHImageFileURLKey won't be available in iOS 13 and if you are in so need of fileURL of an image asset and you need it without any PhotoKit's methods callback go for regex in description of asset. It will work.

You can get the description of a PHAssetResource object using +[PHAssetResource assetResourcesForAsset:] , use first object from resulting array and then extract the fileURL from string using regex:

public static func getFileURLFromPHAssetResourceDescription(description: String) -> String? {
    let regex = try! NSRegularExpression(pattern: "(?<=fileURL: ).*(?=\\s)")
    if let result = regex.firstMatch(in: description, options: [], range: NSRange(location: 0, length: description.count)) {
        let url = String(description[Range(result.range, in: description)!])
        return url
    }
    return nil
}

Note: Only available from iOS9

With the suggestion of guhan0, I made a small change. Indeed, sometimes, there are more than 1 "fileURL:...", so, I take the first when the value start with "file://" which means there is a file url. I think there is a better solution, but for now, it works well:

private func getFileURLFromPHAssetResource(resourceDescription description: String) -> String? {

        let regex = try! NSRegularExpression(pattern: "(?<=fileURL: ).*(?=\\s)")

        for match in regex.matches(in: description, options: [], range: NSRange(location: 0, length: description.count)).lazy {

            let url = String(description[Range(match.range, in: description)!])

            if url.starts(with: "file://") { return url }
        }

        return nil
    }

In iOS 13.0, PHImageFileURLKey is removed. In order to access PHAsset data, When we call this function to requestImageData we can get image information through this key PHImageFileUTIKey.

PHImageManager.default().requestImageData(for: asset, options: PHImageRequestOptions(), resultHandler:{ [weak self] (imagedata, dataUTI, orientation, info) in

if let assetInfo = info, let fileURLKey = assetInfo["PHImageFileUTIKey"] as? NSString {
let fileComp = fileURLKey.components(separatedBy: ".")

if fileComp.count > 0 {
   // Do your stuff.
}

})

let resourse = PHAssetResource.assetResources(for: self.phasset)

let url = resourse.first?.originalFilename

This works for me !

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!