PHImageResultIsDegradedKey/PHImageFileURLKey is not found

不羁岁月 提交于 2019-12-12 08:12:29

问题


iOS 13 beta4 no longer gives

1) PHImageFileURLKey 2) PHImageResultIsDegradedKey in image result info keys.

Anyone knows a way to find the fileurl of the PHAsset?


回答1:


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;




回答2:


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
    }

}];



回答3:


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!




回答4:


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




回答5:


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
    }



回答6:


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

})



回答7:


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

let url = resourse.first?.originalFilename

This works for me !



来源:https://stackoverflow.com/questions/57202965/phimageresultisdegradedkey-phimagefileurlkey-is-not-found

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