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

ImageProcessor to intercept the UIImage #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

fabianthdev
Copy link

This PR introduces the concept of an ImageProcessor to AsyncImage.

Currently, the UIImage loaded by the ImageLoader is directly converted into a SwiftUI Image before it is leaves the ImageProcessor.loadImage(), thus making it unavailable to the app implementing the AsyncImage. Therefore, no changes to the image itself can be made.

The ImageProcessor is a protocol specifying only one function: func process(image: UIImage) -> UIImage. If a class implementing this protocol is supplied to the AsyncImage, this function will be called before the UIImage is converted into a SwiftUI Image. In this function one has unrestricted access to the UIImage.

Because the ImageProcessor processes the image after it has been added to the cache, only the original version of the image is saved to the cache. This means that other AsyncImage instances that do not use the same ImageProcessor can get the original image, while AsyncImage instances that use the same ImageProcessor do not amplify their changes to the same image over time.

Also, I've removed the unnecessary debug print statement from the code.

Discussion
While this prevents the AsyncImage from being a drop-in replacement for Apple's official implementation when replacing this AsyncImage with Apple's due to the additional initializer parameter, the ImageProcessor is completely optional and can be omitted from the initializer call of not needed.
However, I argue that the flexibility achieved by having access to the UIImage far outweighs this drawback, and may also provide a differentiator agains Apple's AsyncImage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant