Skip to content

Commit ff5ffb0

Browse files
appdenchristophpurrer
authored andcommitted
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.
1 parent 84c0863 commit ff5ffb0

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Libraries/Image/RCTImageView.mm

Lines changed: 8 additions & 6 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)
@@ -211,6 +213,10 @@ - (void)updateWithImage:(UIImage *)image
211213
#else // [TODO(macOS GH#774)
212214
image.capInsets = _capInsets;
213215
image.resizingMode = NSImageResizingModeTile;
216+
} else if (_resizeMode == RCTResizeModeCover) {
217+
if (!NSEqualSizes(self.bounds.size, NSZeroSize)) {
218+
image = RCTFillImagePreservingAspectRatio(image, self.bounds.size, self.window.backingScaleFactor ?: 1.0);
219+
}
214220
#endif // ]TODO(macOS GH#774)
215221
} else if (!UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, _capInsets)) {
216222
// Applying capInsets of 0 will switch the "resizingMode" of the image to "tile" which is undesired
@@ -233,17 +239,13 @@ - (void)setImage:(UIImage *)image
233239
{
234240
image = image ?: _defaultImage;
235241
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)
242+
_image = image; // TODO(macOS GH#774)
241243
[self updateWithImage:image];
242244
}
243245
}
244246

245247
- (UIImage *)image {
246-
return _imageView.image;
248+
return _image ?: _imageView.image; // TODO(macOS GH#774)
247249
}
248250

249251
- (void)setBlurRadius:(CGFloat)blurRadius

0 commit comments

Comments
 (0)