How to cache image for temporary use iOS Swift 5

Welcome back to my blog, this week I was too busy to do so many things. I've almost forgot to post new article this week. So I look my old problem that I've already solved and found a problem how to cache image for get dynamic height of UITableview.

I have a problem to reuse UITableViewCell for inserting the height of each cell. I want my cell to display flexible height for UIImageView. And the image itself is fetched from API server which is to need internet to get it and it's not from local. Sometimes it takes a long time to get big size image.

For the very first time to open an app. It will be harder to cache all of images. So if the user doesn't have good internet connection. Cache all of images is good solution, but it will not automatically resolve our issues. For me there's still a little issue when I want to make my height of UIImageView flexible in UITableView. If I already get the height of image, I need to reload the spesific of uitableview row. And it will make weird for UX.

So I create a class called ImageCache, I saved the class inside my Utils Folder to tidy up my project structure. Don't forget to import UIIKit because I used UIImage for parameter. First you need to store the image with two parameters urlString and image. Remember urlString is key we need to get our cache images back. So url is good solution to pass it.

And to get image we just need to pass urlString parameter and it will return UIImage which is we just need to set it in our UIImageView or something else we want. You can customize as you want for this class. And don't forget we use UserDefault to save dictionary of our images and urls. So we uninstall our app it would delete our UserDefault for saving dictionary and Temporary Directory for saving our images. This is not permanent solution because it would be bad to our app. Making our app size bigger.

I got this reference from youtube tutorial that I've forgotten the link. I hope you can get better understanding to cache image. Thanks for visiting my simple blog. Sorry for late posting because I'm learning new things and creating my first iOS project to release it soon in App Store.

Popular posts from this blog

How to restart the app with flutter Android and iOS

How to have ConstraintLayout inside ScrollView and ScrollView inside ConstraintLayout Android

Missing system image Android Studio solution