@@ -48,11 +48,6 @@ @implementation RCTViewComponentView {
4848 __weak CALayer *_borderLayer;
4949 CALayer *_outlineLayer;
5050 CALayer *_boxShadowLayer;
51- #if TARGET_OS_OSX // [macOS
52- UIImage *_boxShadowImage; // Strong reference to keep CGImage valid for layer.contents
53- UIImage *_borderImage; // Strong reference to keep CGImage valid for layer.contents
54- UIImage *_outlineImage; // Strong reference to keep CGImage valid for layer.contents
55- #endif // macOS]
5651 CALayer *_filterLayer;
5752 NSMutableArray <CALayer *> *_backgroundImageLayers;
5853 BOOL _needsInvalidateLayer;
@@ -837,29 +832,16 @@ static RCTCornerRadii RCTCreateOutlineCornerRadiiFromBorderRadii(const BorderRad
837832}
838833
839834// To be used for CSS properties like `border` and `outline`.
840- // On macOS, outImage is set to the generated UIImage so the caller can keep a strong reference.
841835static void RCTAddContourEffectToLayer (
842836 CALayer *layer,
843837 const RCTCornerRadii &cornerRadii,
844838 const RCTBorderColors &contourColors,
845839 const UIEdgeInsets &contourInsets,
846- const RCTBorderStyle &contourStyle
847- #if TARGET_OS_OSX
848- , UIImage * __strong *outImage // [macOS]
849- #endif
850- )
840+ const RCTBorderStyle &contourStyle)
851841{
852842 UIImage *image = RCTGetBorderImage (
853843 contourStyle, layer.bounds .size , cornerRadii, contourInsets, contourColors, [RCTUIColor clearColor ], NO ); // [macOS]
854844
855- #if TARGET_OS_OSX // [macOS
856- // Return the image to the caller so they can keep a strong reference.
857- // This prevents the CGImage from being deallocated while the layer uses it.
858- if (outImage) {
859- *outImage = image;
860- }
861- #endif // macOS]
862-
863845 if (image == nil ) {
864846 layer.contents = nil ;
865847 } else {
@@ -872,7 +854,9 @@ static void RCTAddContourEffectToLayer(
872854 layer.contents = (id )image.CGImage ;
873855 layer.contentsScale = image.scale ;
874856#else // [macOS
875- layer.contents = (__bridge id )UIImageGetCGImageRef (image);
857+ // RCTUIImage caches its CGImage, so it stays valid as long as the image is alive.
858+ // The image is retained by the layer.contents assignment.
859+ layer.contents = (__bridge id )image.CGImage ;
876860 layer.contentsScale = UIImageGetScale (image);
877861#endif // macOS]
878862
@@ -1206,9 +1190,6 @@ - (void)invalidateLayer
12061190 if (useCoreAnimationBorderRendering) {
12071191 [_borderLayer removeFromSuperlayer ];
12081192 _borderLayer = nil ;
1209- #if TARGET_OS_OSX
1210- _borderImage = nil ; // [macOS] Clear image reference when not using custom border layer
1211- #endif
12121193
12131194 layer.borderWidth = (CGFloat)borderMetrics.borderWidths .left ;
12141195 RCTUIColor *borderColor = RCTUIColorFromSharedColor (borderMetrics.borderColors .left ); // [macOS]
@@ -1243,19 +1224,12 @@ - (void)invalidateLayer
12431224 RCTCornerRadiiFromBorderRadii (borderMetrics.borderRadii ),
12441225 borderColors,
12451226 RCTUIEdgeInsetsFromEdgeInsets (borderMetrics.borderWidths ),
1246- RCTBorderStyleFromBorderStyle (borderMetrics.borderStyles .left )
1247- #if TARGET_OS_OSX
1248- , &_borderImage // [macOS] Keep strong reference to prevent CGImage deallocation
1249- #endif
1250- );
1227+ RCTBorderStyleFromBorderStyle (borderMetrics.borderStyles .left ));
12511228 }
12521229
12531230 // outline
12541231 [_outlineLayer removeFromSuperlayer ];
12551232 _outlineLayer = nil ;
1256- #if TARGET_OS_OSX
1257- _outlineImage = nil ; // [macOS] Clear image reference when outline is removed
1258- #endif
12591233 if (_props->outlineWidth != 0 ) {
12601234 if (!_outlineLayer) {
12611235 CALayer *outlineLayer = [CALayer new ];
@@ -1280,11 +1254,7 @@ - (void)invalidateLayer
12801254 RCTCreateOutlineCornerRadiiFromBorderRadii (borderMetrics.borderRadii , _props->outlineWidth ),
12811255 RCTBorderColors{outlineColor, outlineColor, outlineColor, outlineColor},
12821256 UIEdgeInsets{_props->outlineWidth , _props->outlineWidth , _props->outlineWidth , _props->outlineWidth },
1283- RCTBorderStyleFromOutlineStyle (_props->outlineStyle )
1284- #if TARGET_OS_OSX
1285- , &_outlineImage // [macOS] Keep strong reference to prevent CGImage deallocation
1286- #endif
1287- );
1257+ RCTBorderStyleFromOutlineStyle (_props->outlineStyle ));
12881258 }
12891259 }
12901260
@@ -1339,9 +1309,6 @@ - (void)invalidateLayer
13391309 // box shadow
13401310 [_boxShadowLayer removeFromSuperlayer ];
13411311 _boxShadowLayer = nil ;
1342- #if TARGET_OS_OSX // [macOS
1343- _boxShadowImage = nil ; // Release previous image
1344- #endif // macOS]
13451312 if (!_props->boxShadow .empty ()) {
13461313 _boxShadowLayer = [CALayer layer ];
13471314 [self .layer addSublayer: _boxShadowLayer];
@@ -1358,10 +1325,8 @@ - (void)invalidateLayer
13581325#if !TARGET_OS_OSX // [macOS]
13591326 _boxShadowLayer.contents = (id )boxShadowImage.CGImage ;
13601327#else // [macOS
1361- // Keep a strong reference to the NSImage so that the CGImage it provides
1362- // (via UIImageGetCGImageRef) remains valid while the layer uses it.
1363- _boxShadowImage = boxShadowImage;
1364- _boxShadowLayer.contents = (__bridge id )UIImageGetCGImageRef (_boxShadowImage);
1328+ // RCTUIImage caches its CGImage, so it stays valid as long as the image is alive.
1329+ _boxShadowLayer.contents = (__bridge id )boxShadowImage.CGImage ;
13651330#endif // macOS]
13661331 }
13671332
0 commit comments