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
?