0

My code used ALAssetsLibrary to save image and video to iOS gallery. But Xcode tells me that ALAssetsLibrary is deprecated as of iOS 9.0, and advise me to use PHPhotoLibrary instead.

With the old ALAssetsLibrary, I get an Asset URL after saving, something like assets-library://asset/asset.JPG?id=XXX. But with ALAssetsLibrary, I find no easy way to get the asset URL back. A lot of relevant questions on SO refer to this code:

https://gist.github.com/jVirus/2f041bb8b1936093773f0bde42af3a49

But the URL returned is file URL: file://path/to/file, not the asset URL.

The best option I find is to get Asset Identifier and concatenate String:

return URL(string: "assets-library://asset/asset.JPG?id="+assetId+"&ext=JPG")

This is an unsafe hack, as the URL format may change in the future. And sometimes I found the Asset Identifier includes "/" segments at the end, that need to be stripped away before concatenation.

Is there any 'official' API method to get the Asset URL of a PHAsset?

NeoWang
  • 17,361
  • 24
  • 78
  • 126
  • Just curious. what do you want to do with the asset URL? (the one `assets-library://asset/asset.JPG?id=XXX`) – HSG Jun 24 '19 at 08:15
  • @HSG Some SDKs require Asset URLs, like Facebook and Twitter SDK. – NeoWang Jun 24 '19 at 11:21
  • So maybe you want to get the image url to share, can see if https://gist.github.com/dddnuts/ede04e65193d5df6f5b901c004adc7c3 works for you? the code to get a sharable url of an assets – HSG Jun 24 '19 at 21:43

0 Answers0