Skip to content

Commit f092d78

Browse files
christophpurrerappden
authored andcommitted
Fix resizeMode="cover" when image is resized (microsoft#1273)
* Fix resizeMode="cover" when image is resized Since `RCTResizeModeCover` is implemented manually on macOS (iOS benefits from `UIViewContentModeScaleAspectFill` API), we need to reprocess the image any time the bounds of the view update. The `updateImage:` method was already called on resize, so this instead stores the original image in an ivar so it can call `RCTFillImagePreservingAspectRatio` from `updateImage:` instead. * Fix rendering template images with tintColor This fixes a regression introduced by the previous commit, where images would no longer render with the `tintColor` on release builds. We must call `setTemplate:` (using `.template` property is illegal in Objective-C++ since `template` is reserved in C++) on the newly resized image instead of the original image. Confirmed icons look correct in the release build of Messenger Desktop. Co-authored-by: Scott Kyle <skyle@fb.com>
1 parent e1c7801 commit f092d78

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

Libraries/Image/RCTImageView.mm

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ @implementation RCTImageView
128128
// Whether the latest change of props requires the image to be reloaded
129129
BOOL _needsReload;
130130

131+
UIImage *_image; // TODO(macOS GH#774)
132+
131133
RCTUIImageViewAnimated *_imageView;
132134

133135
#if TARGET_OS_OSX // [TODO(macOS GH#774)
@@ -199,10 +201,6 @@ - (void)updateWithImage:(UIImage *)image
199201
if (_renderingMode != image.renderingMode) {
200202
image = [image imageWithRenderingMode:_renderingMode];
201203
}
202-
#else // [TODO(macOS GH#774)
203-
if ((_renderingMode == UIImageRenderingModeAlwaysTemplate) != [image isTemplate]) {
204-
[image setTemplate:(_renderingMode == UIImageRenderingModeAlwaysTemplate)];
205-
}
206204
#endif // ]TODO(macOS GH#774)
207205

208206
if (_resizeMode == RCTResizeModeRepeat) {
@@ -211,6 +209,10 @@ - (void)updateWithImage:(UIImage *)image
211209
#else // [TODO(macOS GH#774)
212210
image.capInsets = _capInsets;
213211
image.resizingMode = NSImageResizingModeTile;
212+
} else if (_resizeMode == RCTResizeModeCover) {
213+
if (!NSEqualSizes(self.bounds.size, NSZeroSize)) {
214+
image = RCTFillImagePreservingAspectRatio(image, self.bounds.size, self.window.backingScaleFactor ?: 1.0);
215+
}
214216
#endif // ]TODO(macOS GH#774)
215217
} else if (!UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, _capInsets)) {
216218
// Applying capInsets of 0 will switch the "resizingMode" of the image to "tile" which is undesired
@@ -222,6 +224,12 @@ - (void)updateWithImage:(UIImage *)image
222224
#endif // ]TODO(macOS GH#774)
223225
}
224226

227+
#if TARGET_OS_OSX // [TODO(macOS GH#774)
228+
if ((_renderingMode == UIImageRenderingModeAlwaysTemplate) != [image isTemplate]) {
229+
[image setTemplate:(_renderingMode == UIImageRenderingModeAlwaysTemplate)];
230+
}
231+
#endif // ]TODO(macOS GH#774)
232+
225233
// Apply trilinear filtering to smooth out mis-sized images
226234
_imageView.layer.minificationFilter = kCAFilterTrilinear;
227235
_imageView.layer.magnificationFilter = kCAFilterTrilinear;
@@ -233,17 +241,13 @@ - (void)setImage:(UIImage *)image
233241
{
234242
image = image ?: _defaultImage;
235243
if (image != self.image) {
236-
#if TARGET_OS_OSX // [TODO(macOS GH#774)
237-
if (image && _resizeMode == RCTResizeModeCover && !NSEqualSizes(self.bounds.size, NSZeroSize)) {
238-
image = RCTFillImagePreservingAspectRatio(image, self.bounds.size, self.window.backingScaleFactor ?: 1.0);
239-
}
240-
#endif // ]TODO(macOS GH#774)
244+
_image = image; // TODO(macOS GH#774)
241245
[self updateWithImage:image];
242246
}
243247
}
244248

245249
- (UIImage *)image {
246-
return _imageView.image;
250+
return _image ?: _imageView.image; // TODO(macOS GH#774)
247251
}
248252

249253
- (void)setBlurRadius:(CGFloat)blurRadius

0 commit comments

Comments
 (0)