kmell96
kmell96

Reputation: 1465

iOS adding images to UITableView makes it take too long to display

I have a UIViewController that controls a simple page with one UITableView in my app. The table has 12 rows. I just added an image (40px by 40px) to each row with the following simple line of code in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath):

cell.imageView?.image = UIImage(named: "<image_name_for_row>")

After doing this, the time it takes for the page to display increased by about 3x. This is the only change I made to the code. After displaying the page for the first time, if I display the same instance of the view controller again, it displays almost instantly; essentially as fast as it used to before I added the images. So, I can only assume that the images get cached somewhere after the page displays for the first time... is there a way to cache the images when the app loads to avoid the very noticeable delay the first time I show the view?

I tried putting the following code in application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?):

menuViewController.view.setNeedsLayout()

But, this didn't decrease the page load time. This also failed to decrease the load time:

_ = UIImage(named: "menu_row_1")
...
_ = UIImage(named: "menu_row_12")

Any suggestions?

Upvotes: 0

Views: 324

Answers (1)

Puneet Sharma
Puneet Sharma

Reputation: 9484

There is a known performance impact while using [init(named:)] method. Try using this instead. Just pass nil for bundle and traitCollection args:

init(named:in:compatibleWith:)

Upvotes: 2

Related Questions