6

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?

guhan0
  • 666
  • 6
  • 19

9 Answers9

3

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;
dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
Slyv
  • 421
  • 3
  • 8
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
    }

}];
AEQ
  • 1,339
  • 1
  • 16
  • 20
1

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!

Amir Hajiha
  • 836
  • 8
  • 20
  • Make sure to call contentEditingInput.Dispose(); or the app will crash when called with many photos. – KTrace Dec 22 '19 at 19:48
1

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

let url = resourse.first?.originalFilename

This works for me !

xiaochuan
  • 11
  • 1
0

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

guhan0
  • 666
  • 6
  • 19
0

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
    }
Lapinou
  • 1,467
  • 2
  • 20
  • 39
0

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

})
Bhanu
  • 1,249
  • 10
  • 17
  • the PHImageFileUTIKey is not a replacement for PHImageFileURLKey, there is no correlation between them, what's the point of having the value of PHImageFileUTIKey then? – XcodeNOOB Jan 07 '20 at 08:12
0

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();

});
KTrace
  • 486
  • 1
  • 5
  • 12
0

You can use

let photoPath = photoAsset.localIdentifier

to replace the

let photoPath = info["PHImageFileURLKey"]