Skeptick / libres

Resources generation in Kotlin Multiplatform
Apache License 2.0
204 stars 12 forks source link

UIImage cache #15

Open vvvladvvvolkovvvskiy opened 1 year ago

vvvladvvvolkovvvskiy commented 1 year ago

UIImage.imageNamed cached. When use a lot of images, cache eating a lot of ram

Apple says : If you intend to display an image only once and don't want it added to the system’s cache, create it using the [imageWithContentsOfFile:]

Ideal have option, that indicate reusage images Don't know how fix.

  1. Clear cache at private access.
  2. Maybe don't need to have val(use function) and gc clear it(need to test)
  3. Or provide something like url(if possible)

Sorry, i'm android developer and don't have a lot of expirience to help with ios

vvvladvvvolkovvvskiy commented 1 year ago

Created own UIImage not from generated kotlin object. Clear don't work, cache depends on lifecycle app

Skeptick commented 1 year ago

Hello! In which case did you encounter this problem? When using Compose / UIKit / both? Can you share reproducer project? It would be very helpful in understanding the problem and finding solution.

vvvladvvvolkovvvskiy commented 1 year ago

Using SwiftUI. For example : App contains 30 images App navigation : Screen1 -> Screen2 -> Screen3. (my case, struct Screen[1,2,3] : View) Each screen have horizontal pager with unique images(Screen1 contains from 0 to 10 images, Screen2 from 10 to 20, ...) Use Image(uiImage : Res.shared.images.customImage) While navigate between pages image load, cached and increase memory At Screen3, previous screens decomposed, but memory not cleared and contains previously images

Found that UIImage(named:) caches images, and UIImage(contentsOfFile:) does not. And when lifecycle goes to background cache of images cleared.

To check ram, used XCode -> Debug Navigator -> Memory -> Memory use

Skeptick commented 1 year ago

Okay, I see. As a possible solution, I can add object url containing URLs to images, so then image can be created using UIImage(contentsOfFile: Res.shared.image.url.customImage)

Or a new object assets (as part of future raw file functionality), then UIImage(contentsOfFile: Res.shared.asset.image.customImage)

I prefer the second option as the first one can break backwards compatibility (if project has an image called url).

I'll think about it.

vvvladvvvolkovvvskiy commented 1 year ago

Thanks for help