Skip to content

Commit

Permalink
Always use the correct representation when drawing image
Browse files Browse the repository at this point in the history
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 TextureGroup#1663).  Whenever
the image node is displayed we need to make sure we are generating the
correct representation.
  • Loading branch information
bdolman committed Nov 24, 2021
1 parent e3bdf89 commit fa9bbe8
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 17 deletions.
17 changes: 1 addition & 16 deletions Source/ASImageNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion Source/Private/ASImageNode+AnimatedImagePrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit fa9bbe8

Please sign in to comment.