I have a problem where I am creating UIImageAsset objects to store multiple sizes of images downloaded from an API, but the memory allocated to the UIImageAssets does not free up any more.
In my testing this only happens with downloaded UIImages, not with those from the xcassets catalog using UIImage(named:) initializer.
In this example I am just using a random cat image from the internet, but that does the trick too.
func createImageAsset() {
var scale = 1
var asset = UIImageAsset()
for _ in 0...5 {
var data = Data()
do {
try data = Data(contentsOf: URL(string: "https://i.pinimg.com/originals/21/94/3b/21943bb9849d304ef77850a9df2d3020.png")!)
} catch {
print(error.localizedDescription)
}
asset.register(UIImage(data: data)!, with: UITraitCollection(displayScale: CGFloat(scale)))
scale += 1
}
}
To clear up why I use UIImageAsset: I'm trying to replicate behavior of the xcassets catalog to let the UIImageView get an appropriately sized image from this asset. Suggestions of other solutions are appreciated, but I am primarily looking for solutions/explanations for this memory issue.