Skip to content

Conversation

lukmccall
Copy link
Contributor

New Pull Request Checklist

  • I have read and understood the CONTRIBUTING guide

  • I have read the Documentation

  • I have searched for a similar pull request in the project and found none

  • I have updated this branch with the latest master to avoid conflicts (via merge from master or rebase)

  • I have added the required tests to prove the fix/feature I am adding

  • I have updated the documentation (if necessary)

  • I have run the tests and they pass

  • I have run the lint and it passes (pod lib lint)

This merge request fixes:
sd_imageFormat returns nil when used on SDAnimatedImage to which static image were loaded.

Pull Request Description

When a static image is loaded into SDAnimatedImage, the sd_imageFormat remains unset. As a result, the associated object on the underlying UIImage is not present. This value is typically assigned by decoders; for instance, SDImageIOCoder does so here. However, with SDAnimatedImage, the correct format is returned only if the animatedImageData is included.

I’m uncertain if my solution is accurate (I’ve tested it, but I’m unsure if it’s the right approach). I’ve opened this PR to initiate a discussion on how to properly address that issue.

@@ -182,6 +182,8 @@ - (instancetype)initWithData:(NSData *)data scale:(CGFloat)scale options:(SDImag
#else
self = [super initWithCGImage:image.CGImage scale:MAX(scale, 1) orientation:image.imageOrientation];
#endif
// Defines the associated object that holds the format for static images
super.sd_imageFormat = format;
Copy link
Contributor

@dreampiggy dreampiggy Oct 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems it's better to just to override the sd_imageFormat and return the animatedImageFormat

Oh, I've already use this 2 years ago...

@dreampiggy
Copy link
Contributor

dreampiggy commented Oct 19, 2024

It's a history issue of SDAnimatedImage. It was not designed to support Static Image at the beginning for long years, but someday, a user complains about this and wants this feature. So I added static image support in #3626

Some of sd_XXX property is not designed to run on SDAnimatedImage in my original design. The original design from me is to always check with protocol or subclass from the caller...

SDImageFormat format;
UIImage *image;
if ([image isKindOfClass:SDAnimatedImage.class]) {
    format = [image animatedImageFormat];
} else {
    format = image.sd_imageFormat;
}

After #3626, I've fixed some of them, but not all of them. It's OK to merge this PR, but I doubt this will make things more and more difficult to maintain. Should all the sd_XXX property works on SDAnimatedImage ?

Because in some use case, SDAnimatedImage is different from UIImage, we can not hide the implementation details between these two classes. So as user, you should better take care of this specify type by your hand, especially when you want to query frames, data buffer and other metadata which only available on animated format (not available on UIImage class from UIKit)

@dreampiggy
Copy link
Contributor

dreampiggy commented Oct 19, 2024

By the way, your comment here:

When a static image is loaded into SDAnimatedImage, the sd_imageFormat remains unset

Is not correct.

Check the UIImage.sd_imageFormat implementation, it will fallback to use https://developer.apple.com/documentation/coregraphics/1456067-cgimagegetuttype . Which can actually get the UTType and convert to SDImageForamt

Only if you use the Force Decoding feature, which will remove the ImageIO backed property and cause this UTType missing. (Force Decoding defaults to ON, you can use SDImageCoderHelper.ForceDecodePolicy to control the behavior)

@@ -375,7 +377,8 @@ - (SDImageFormat)sd_imageFormat {
}

- (void)setSd_imageFormat:(SDImageFormat)sd_imageFormat {
Copy link
Contributor

@dreampiggy dreampiggy Oct 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This override method does nothing (The same as super implementation actually). You can just remove this function at all, which has the same effect.

@dreampiggy
Copy link
Contributor

It's OK I can just merge this, and remove that setSd_imageFormat: override method by my hand...

@lukmccall
Copy link
Contributor Author

Thank you for reviewing my PR. I can certainly remove that override. I mainly left it there to comment on the situations in which the user might expect it to be triggered.

@lukmccall lukmccall requested a review from dreampiggy October 21, 2024 09:27
@dreampiggy dreampiggy merged commit 780aa6d into SDWebImage:master Oct 21, 2024
6 of 7 checks passed
@dreampiggy dreampiggy changed the title Fix sd_imageFormat sometimes returns undefined Fix sd_imageFormat sometimes returns undefined on static image Nov 1, 2024
@dreampiggy dreampiggy modified the milestones: 6.0.0, 5.20.0 Nov 1, 2024
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.

2 participants