From a95075b54daf5e1e66bdfe8cd871981f956d9dca Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Tue, 14 Nov 2023 16:10:51 +0800 Subject: [PATCH] Fix the iOS 17 UIKit behavior issue that UIImageView entering the background will reset CALayer's contents, which cause animated image render issue Should use traitCollectionDidChange to refresh the CALayer's contents status to match current frame index --- SDWebImage/Core/SDAnimatedImageView.m | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/SDWebImage/Core/SDAnimatedImageView.m b/SDWebImage/Core/SDAnimatedImageView.m index db7d7675..5b719ff4 100644 --- a/SDWebImage/Core/SDAnimatedImageView.m +++ b/SDWebImage/Core/SDAnimatedImageView.m @@ -501,6 +501,17 @@ } } +#if SD_UIKIT +- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection { + // See: #3635 + // From iOS 17, when UIImageView entering the background, it will receive the trait collection changes, and modify the CALayer.contents by `self.image.CGImage` + // However, For animated image, `self.image.CGImge != self.currentFrame.CGImage`, right ? + // So this cause the render issue, we need to reset the CALayer.contents again + [super traitCollectionDidChange:previousTraitCollection]; + [self.imageViewLayer setNeedsDisplay]; +} +#endif + #if SD_MAC // NSImageView use a subview. We need this subview's layer for actual rendering. // Why using this design may because of properties like `imageAlignment` and `imageScaling`, which it's not available for UIImageView.contentMode (it's impossible to align left and keep aspect ratio at the same time)