question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support returning ph:// from MediaLibrary APIs

See original GitHub issue

Summary

On iOS Assets Library API is depreacted: https://developer.apple.com/documentation/assetslibrary. The MediaLibrary still uses the old assets-library:// schema. It is incompatible with a few APIs like the sharing API and the FaceDetection API.

Found this comment here that alerted me to that: https://github.com/react-native-cameraroll/react-native-cameraroll/blob/master/ios/RNCAssetsLibraryRequestHandler.m#L68.

After a Twitter conversation we found these lines responsible: https://github.com/expo/expo/blob/fffbdb3161449764087dfa6d555ad1f88eb86dfe/packages/expo-media-library/ios/EXMediaLibrary/EXMediaLibrary.m#L920-L926

I was quite confused about it as you can see here: https://stackoverflow.com/questions/67422493/getting-a-filesystem-uri-from-assets-in-expo-rn.

The only workaround I found from the JS API was to copy assets to a cache directory. This is less than ideal as it slows things down quite a bit.

Managed or bare workflow? If you have ios/ or android/ directories in your project, the answer is bare!

managed

What platform(s) does this occur on?

iOS

SDK Version (managed workflow only)

41.0

Environment

N/A

Reproducible demo or steps to reproduce from a blank project

N/A

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ajsmthcommented, Aug 24, 2021

hey there - I’ve been looking into this issue a bit today - I think the method you might want to use here is MediaLibrary.getAssetInfoAsync(asset) which will return a localUri containing the file:// url that can be used with other APIs. This method obtains a local image url via PHAsset.requestContentEditingInputWithOptions async block

An alternative, as you have noted in SO, would be using something like PHImageManager requestImageDataForAsset after obtaining the PHAsset - although this is not done by the MediaLibrary module so you’d have to write your own implementation for that.

In either case, updating the url here does not immediately solve any integration issues that you’ve described, as most of these APIs require the file:// localUri from a PHAsset, something that needs to be asynchronously fetched one way or another. That being said I think it’s safe to update the asset-library:// to ph:// url and I’ll try and get this into SDK 43

In the meantime, the an asset.id can be used to generate the same ph:// url like so:

function getPhotoKitUrl(asset) {
   return `ph://${asset.id.split("/")[0]}`
}
1reaction
StampixSMOcommented, Jul 12, 2021

I ended up just rewriting the asset-library:// URIs to ph:// URIs by grabbing the ID from the former’s query parameters & appending it to the latter.

I did have to install an SDWebImage plugin to support ph:// URIs though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Trying to retrieve localUri from assets in Expo MediaLibrary ...
I have a ReactNative Expo app that loads recent images from a users media library. For this I need to use the MediaLibrary.getAssetsAsync() ......
Read more >
MediaLibrary - Expo Documentation
Supported keys that can be used to sort getAssetsAsync results. ... Returns whether the Media Library API is enabled on the current device....
Read more >
Michael Yagudaev ‍ on Twitter: "@Koprowski_it Do you happen to ...
Called into the FaceDetection Apple API (Vision Framework) -- so far, ... Support returning ph:// from MediaLibrary APIs · Issue #12903 · expo/expo....
Read more >
Features and APIs Overview | Android Developers
Starting with Android 13, you can opt in to themed app icons. With this feature, app icons in supported Android launchers are tinted...
Read more >
Add custom class to attachment in media library grid mode
The classes added to each element in the media library grid are ... Library.extend({ className: function () { return 'attachment ' + ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found