From f332fac16346d2f03d056575cc988a0b2bbb48c6 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen Date: Wed, 11 Mar 2020 17:58:01 -0700 Subject: [PATCH] iOS: Add UIScene support to RCTImageView (#28141) Summary: Apps implementing `UISceneDelegate` no longer clear out images when backgrounded because `UIApplicationDidEnterBackgroundNotification` no longer gets fired. ## Changelog [iOS] [Added] - UIScene support for RCTImageView Pull Request resolved: https://github.com/facebook/react-native/pull/28141 Test Plan: 1. Create a new iOS app implementing `UISceneDelegate` or modify an existing one 2. Open a React view with some images 3. Switch to another app, or background the current app 4. Observe that `-[RCTImageView clearImageIfDetached]` gets called Reviewed By: shergin Differential Revision: D20009200 Pulled By: hramos fbshipit-source-id: bdbf79d6cf56a295344c036b9225efec672fa780 --- Libraries/Image/RCTImageView.mm | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Libraries/Image/RCTImageView.mm b/Libraries/Image/RCTImageView.mm index 38267ebb98..07ffe3acc2 100644 --- a/Libraries/Image/RCTImageView.mm +++ b/Libraries/Image/RCTImageView.mm @@ -86,6 +86,10 @@ static NSDictionary *onLoadParamsForSource(RCTImageSource *source) { if ((self = [super initWithFrame:CGRectZero])) { _bridge = bridge; + _imageView = [[RCTUIImageViewAnimated alloc] init]; + _imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; + [self addSubview:_imageView]; + NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center addObserver:self selector:@selector(clearImageIfDetached) @@ -95,9 +99,15 @@ static NSDictionary *onLoadParamsForSource(RCTImageSource *source) selector:@selector(clearImageIfDetached) name:UIApplicationDidEnterBackgroundNotification object:nil]; - _imageView = [[RCTUIImageViewAnimated alloc] init]; - _imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; - [self addSubview:_imageView]; +#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 + if (@available(iOS 13.0, *)) { + [center addObserver:self + selector:@selector(clearImageIfDetached) + + name:UISceneDidEnterBackgroundNotification + object:nil]; + } +#endif } return self; }