Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework image cache #1186

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

Rework image cache #1186

wants to merge 7 commits into from

Conversation

wutschel
Copy link
Collaborator

@wutschel wutschel commented Nov 3, 2024

Description

Closes #1176.

This PR does another major improvement to the SDWebImage cache implementation.

Memory improvement with shouldDecompressImages

When enabling the SDWebImage-setting shouldDecompressImages, any UIImage loaded from flash to in-memory retained the CG context of the decompressed image. This PR applies a change to the decompression which results in not retaining this copy anymore. The memory consumption for the image cache is reduced to 50%.

Memory improvement without shouldDecompressImages

When disabling the SDWebImage-setting shouldDecompressImages, any UIImage loaded from flash to in-memory retains the (PNG) data loaded from flash. In this case the memory consumption is reduced to about 60%.

Pre-scaling to target resolution and aspect ratio

After reviewing the implementation in-depth, it became clear that thumb images are not really fully scaled to the target resolution, which would require content fill method to be taken into account at this stage. In fact, the image's aspect ratio was kept, and the thumbs were only scaled half-way to meet to minimum resolution requirements. Examples:

  • Target (300x300), image (600x900) -> scaled to 300x450
  • Target (300x300), image (900x600) -> scaled to 450x300

This caused some scaling do be processed by UIImageView, if target and image have different aspect ratios. In addition, the PNG image saved to flash used more pixels and therefore size. This is prominent especially when target and image aspect ratio mismatch a lot, like for TV station logos in grid view.

This PR now scales images to the exact target resolution and aspect ratio before writing them to flash. The implementation takes into account the UIViewContentMode used by the targeted UIImageView to avoid any image scaling after this initial image caching.

Summary for release notes

Improvement: Reduce memory consumption of image cache
Improvement: Image cache pre-scales to target resolution and aspect ratio
Improvement: Reduce load by avoiding unwanted image scaling / processing

- Only create color space, if needed, to avoid excessive memory consumption when keeping the image size.
- Remove the image parameter as all of this is given by self.
- Remove the quality parameter as callers used same value.
- Let image scaling support different content scale modes
Not setting bytes-per-row somehow does not cause to retain CG context. This reduces the memory usage of the image cache to 50% when having shouldDecompressImages enabled.
This reduces the processing load when loading images from flash to RAM. But it also ends up in retaining the flash data from which the UIImages are created. This increases the image memory cache size by about 25%.
Removes not needed calculations of dimensions.
@wutschel wutschel marked this pull request as ready for review November 8, 2024 06:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rework pre-scaling / caching of images
1 participant