iosswiftuiimagephotosframework

UIImage to PHAsset


I am trying to convert UIImage into PHAsset but could not find any solution. All I found is the vice versa (i.e. PHAsset to UIImage).

The scenario is, I am fetching images from a custom directory into PHAssetCollection and showing in UICollectionView. I also want to include few more images here from previous screen which are coming from remote source. I don't want to persist remote images into my directory but want to include them in my UICollectionView.

Kindly suggest me solution or some good alternate so that the source of UICollectionView will be same (i.e. PHAssetCollection)


Solution

  • You do not want to convert a PHAsset to UIImage. I do not know if it is possible; it shouldn't be, since PHAsset and UIImage have different properties, behaviours, etc. The vice versa is possible because all the properties of UIImage can be derived from PHAsset but not vice-versa.

    Instead, change the data source of the collection view to [UIImage] while converting PHAsset to UIImage instead. This approach is clean and solves your other problem of having to convert UIImage to PHImage.