vinhnx / notes

my today I learn (TIL) journal, includes everything that I found interesting, not necessarily relates to programming
43 stars 2 forks source link

Loading a large image on the main thread #689

Open vinhnx opened 8 months ago

vinhnx commented 8 months ago
import UIKit

// Before
let image = UIImage(named: "big-image")
imageView.image = image

// After
let image = UIImage(named: "big-image")

image?.prepareForDisplay { [weak self] preparedImage in
    DispatchQueue.main.async {
        self?.imageView.image = preparedImage
    }
}

// using Swift Concurrency
let image = UIImage(named: "big-image")

Task {
    imageView.image = await image?.byPreparingForDisplay()
}

https://www.swiftwithvincent.com/blog/bad-practice-loading-a-large-image-on-the-main-thread