PHImageResultIsDegradedKey/PHImageFileURLKey is not found

前端 未结 9 861
忘了有多久
忘了有多久 2020-12-09 22:59

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 P

相关标签:
9条回答
  • 2020-12-09 23:29

    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.
    }
    
    })
    
    0 讨论(0)
  • 2020-12-09 23:30

    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

    0 讨论(0)
  • 2020-12-09 23:36

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

    let url = resourse.first?.originalFilename

    This works for me !

    0 讨论(0)
  • 2020-12-09 23:38

    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!

    0 讨论(0)
  • 2020-12-09 23:42

    If anyone needs the file size when using RequestContentEditingInput in Xamarin.

    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();
    
    });
    
    0 讨论(0)
  • 2020-12-09 23:42

    You can use

    let photoPath = photoAsset.localIdentifier
    

    to replace the

    let photoPath = info["PHImageFileURLKey"]
    
    0 讨论(0)
提交回复
热议问题