1

I have some local images from an Asset Catalog that I would like to load ahead of time before displaying them with Image(_ name: String, bundle: Bundle? = nil). Is this possible?

I am trying to optimize performance as much as possible. I have a fairly large background image that would be nice to preload, as well as a number of other large images.

Alex
  • 3,861
  • 5
  • 28
  • 44
  • 1
    I don't know what you are trying to achieve, maybe explain what is the problem with loading images directly to get a proper answer, but you could create `UIImage` using `UIImage(named: String, in: Bundle?)` object and then pass it to `Image(uiImage:)` initializer – timbre timbre May 02 '23 at 15:52
  • @klangfarb Thanks. I added more detail to the post. It is mainly for performance. – Alex May 02 '23 at 16:34
  • 1
    I assume this is using the same caching as `UIImage`. So loading the `UIImage` with same name should preload the image for you. But there is a catch: `UIImage` is unfortunately lazily loaded for some time now. So you need to actually use it before the data is loaded. To do so I find it easiest to simply redraw it to a small size. Try [this one][https://stackoverflow.com/a/68784325/526828] if you can. – Matic Oblak May 02 '23 at 19:34

0 Answers0