繁体   English   中英

未找到 PHImageResultIsDegradedKey/PHImageFileURLKey

[英]PHImageResultIsDegradedKey/PHImageFileURLKey is not found

iOS 13 beta4 不再提供

1) PHImageFileURLKey 2) PHImageResultIsDegradedKey 在图像结果信息键中。

任何人都知道找到PHAsset 的fileurl 的方法吗?

您必须从 PHAsset 对象使用此函数:

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

然后你可以通过这种方式检索 URL:

NSURL *url = contentEditingInput.fullSizeImageURL;

一些示例 Objective-C 代码供其他人寻找:

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

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

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

}];

如果有人希望在 Xamarin.iOS 上实现相同的目标,这是我的 C# 版本的 swift 代码:

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

进而:

private void phcontentediting(PHContentEditingInput contentEditingInput, NSDictionary requestStatusInfo)
        {

        }

phcontentediting时, contentEditingInput 有很多有用的信息,例如CreationDateFullSizeImageUrl甚至Location

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

让 url = resourse.first?.originalFilename

这对我有用!

1. PHImageResultIsDegradedKey现已在最新的 iOS 13 GM 和 GM2 中可用。 所以一个问题解决了。

2. PHImageFileURLKey在 iOS 13 中不可用,如果您非常需要图像资产的 fileURL 并且您需要它而没有任何 PhotoKit 的方法回调,请在资产描述中使用正则表达式。 它会起作用。

您可以使用+[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
}

注意:仅适用于 iOS9

在guhan0的建议下,做了一个小小的改动。 事实上,有时,有不止 1 个“fileURL:...”,所以,当值以“file://”开头时,我取第一个,这意味着有一个文件 url。 我认为有一个更好的解决方案,但就目前而言,它运行良好:

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
    }

在 iOS 13.0 中,移除了PHImageFileURLKey 为了访问 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.
}

})

如果有人在 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();

});

您可以使用

let photoPath = photoAsset.localIdentifier

替换

let photoPath = info["PHImageFileURLKey"]

暂无
暂无

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

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