From fa9bbe8f9767927fc7a553550e23598f92883152 Mon Sep 17 00:00:00 2001 From: Ben Dolman Date: Tue, 23 Nov 2021 19:40:20 -0700 Subject: [PATCH] Always use the correct representation when drawing image MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this change, the image asset (which may contain multiple reps of an image for differing traits) was only consulted when the trait collection changed. But in cases where display was scheduled on the node and the trait collection had not changed, the image node ends up with the default representation instead. An example of this: You have an image asset with two versions: light (default) and dark. The device is currently in dark mode The image is being presented in an item in a collection node. 1. Item node is first added to hierarchy. Trait collection doesn't match so regenerateFromImageAsset=true. 2. Dark image is rendered, as expected 3. Item node is scrolled off screen 4. Item is scrolled back on screen 5. This triggers display on the item node 6. Trait collection has NOT changed so regenerateFromImageAsset=false 7. So, imageAsset is not consulted and default (Light) image is rendered After looking at this a lot, I don’t see any reason why we need the regenerateFromImageAsset flag at all (introduced in #1663). Whenever the image node is displayed we need to make sure we are generating the correct representation. --- Source/ASImageNode.mm | 17 +---------------- .../Private/ASImageNode+AnimatedImagePrivate.h | 1 - 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/Source/ASImageNode.mm b/Source/ASImageNode.mm index 95ac20143..90bf1c5c8 100644 --- a/Source/ASImageNode.mm +++ b/Source/ASImageNode.mm @@ -181,7 +181,6 @@ - (instancetype)init _imageNodeFlags.cropEnabled = YES; _imageNodeFlags.forceUpscaling = NO; - _imageNodeFlags.regenerateFromImageAsset = NO; _cropRect = CGRectMake(0.5, 0.5, 0, 0); _cropDisplayBounds = CGRectNull; _placeholderColor = ASDisplayNodeDefaultPlaceholderColor(); @@ -295,8 +294,7 @@ - (NSObject *)drawParametersForAsyncLayer:(_ASDisplayLayer *)layer ASLockScopeSelf(); UIImage *drawImage = _image; if (AS_AVAILABLE_IOS_TVOS(13, 10)) { - if (_imageNodeFlags.regenerateFromImageAsset && drawImage != nil) { - _imageNodeFlags.regenerateFromImageAsset = NO; + if (drawImage != nil && drawImage.imageAsset != nil) { UITraitCollection *tc = [UITraitCollection traitCollectionWithUserInterfaceStyle:_primitiveTraitCollection.userInterfaceStyle]; UIImage *generatedImage = [drawImage.imageAsset imageWithTraitCollection:tc]; if ( generatedImage != nil ) { @@ -777,19 +775,6 @@ - (NSDictionary *)debugLabelAttributes }; } -- (void)asyncTraitCollectionDidChangeWithPreviousTraitCollection:(ASPrimitiveTraitCollection)previousTraitCollection { - [super asyncTraitCollectionDidChangeWithPreviousTraitCollection:previousTraitCollection]; - - if (AS_AVAILABLE_IOS_TVOS(13, 10)) { - AS::MutexLocker l(__instanceLock__); - // update image if userInterfaceStyle was changed (dark mode) - if (_image != nil - && _primitiveTraitCollection.userInterfaceStyle != previousTraitCollection.userInterfaceStyle) { - _imageNodeFlags.regenerateFromImageAsset = YES; - } - } -} - @end diff --git a/Source/Private/ASImageNode+AnimatedImagePrivate.h b/Source/Private/ASImageNode+AnimatedImagePrivate.h index 917eb6fbb..e41ba34e3 100644 --- a/Source/Private/ASImageNode+AnimatedImagePrivate.h +++ b/Source/Private/ASImageNode+AnimatedImagePrivate.h @@ -28,7 +28,6 @@ unsigned int animatedImagePaused:1; unsigned int cropEnabled:1; // Defaults to YES. unsigned int forceUpscaling:1; //Defaults to NO. - unsigned int regenerateFromImageAsset:1; //Defaults to NO. } _imageNodeFlags; }