简体   繁体   English

未找到 PHImageResultIsDegradedKey/PHImageFileURLKey

[英]PHImageResultIsDegradedKey/PHImageFileURLKey is not found

iOS 13 beta4 no longer gives iOS 13 beta4 不再提供

1) PHImageFileURLKey 2) PHImageResultIsDegradedKey in image result info keys. 1) PHImageFileURLKey 2) PHImageResultIsDegradedKey 在图像结果信息键中。

Anyone knows a way to find the fileurl of the PHAsset?任何人都知道找到PHAsset 的fileurl 的方法吗?

You have to use this function from PHAsset object:您必须从 PHAsset 对象使用此函数:

- (PHContentEditingInputRequestID)requestContentEditingInputWithOptions:(nullable PHContentEditingInputRequestOptions *)options completionHandler:(void (^)(PHContentEditingInput *__nullable contentEditingInput, NSDictionary *info))completionHandler;

Then you can retrieve URL this way:然后你可以通过这种方式检索 URL:

NSURL *url = contentEditingInput.fullSizeImageURL;

Some example Objective-C code for anyone else looking for it:一些示例 Objective-C 代码供其他人寻找:

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:如果有人希望在 Xamarin.iOS 上实现相同的目标,这是我的 C# 版本的 swift 代码:

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 !phcontentediting时, contentEditingInput 有很多有用的信息,例如CreationDateFullSizeImageUrl甚至Location

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

let url = resourse.first?.originalFilename让 url = resourse.first?.originalFilename

This works for me !这对我有用!

1. PHImageResultIsDegradedKey is now being available in latest iOS 13 GM and GM2. 1. PHImageResultIsDegradedKey现已在最新的 iOS 13 GM 和 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. 2. PHImageFileURLKey在 iOS 13 中不可用,如果您非常需要图像资产的 fileURL 并且您需要它而没有任何 PhotoKit 的方法回调,请在资产描述中使用正则表达式。 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:您可以使用+[PHAssetResource assetResourcesForAsset:]获取 PHAssetResource 对象的description ,使用结果数组中的第一个对象,然后使用正则表达式从字符串中提取fileURL

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注意:仅适用于 iOS9

With the suggestion of guhan0, I made a small change.在guhan0的建议下,做了一个小小的改动。 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.事实上,有时,有不止 1 个“fileURL:...”,所以,当值以“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.在 iOS 13.0 中,移除了PHImageFileURLKey In order to access PHAsset data, When we call this function to requestImageData we can get image information through this key PHImageFileUTIKey .为了访问 PHAsset 数据,当我们调用这个函数来 requestImageData 时,我们可以通过这个键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.
}

})

If anyone needs the file size when using RequestContentEditingInput in Xamarin.如果有人在 Xamarin 中使用 RequestContentEditingInput 时需要文件大小。

var options = new PHContentEditingInputRequestOptions();
asset.RequestContentEditingInput(options, (PHContentEditingInput contentEditingInput, NSDictionary requestStatusInfo) =>
{
    var imageUrl = contentEditingInput.FullSizeImageUrl.ToString();

    NSObject fileSizeObj;
    if (contentEditingInput.FullSizeImageUrl.TryGetResource(new NSString("NSURLFileSizeKey"), out fileSizeObj))
    {
        var fileSizeNSNum = fileSizeObj as NSNumber;
        long fileSize = fileSizeNSNum.Int64Value;
    }

    // Make sure to dispose or your app will crash with a lot of photos!
    contentEditingInput.Dispose();

});

You can use您可以使用

let photoPath = photoAsset.localIdentifier

to replace the替换

let photoPath = info["PHImageFileURLKey"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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