diff --git a/Libraries/Image/RCTImageView.m b/Libraries/Image/RCTImageView.m index b805cd9b6..1376fb796 100644 --- a/Libraries/Image/RCTImageView.m +++ b/Libraries/Image/RCTImageView.m @@ -62,10 +62,25 @@ static BOOL RCTShouldReloadImageForSizeChange(CGSize currentSize, CGSize idealSi { if ((self = [super init])) { _bridge = bridge; + + NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; + [center addObserver:self + selector:@selector(clearImageIfDetached) + name:UIApplicationDidReceiveMemoryWarningNotification + object:nil]; + [center addObserver:self + selector:@selector(clearImageIfDetached) + name:UIApplicationDidEnterBackgroundNotification + object:nil]; } return self; } +- (void)dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver:self]; +} + RCT_NOT_IMPLEMENTED(- (instancetype)init) - (void)updateImage @@ -172,6 +187,13 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init) self.image = nil; } +- (void)clearImageIfDetached +{ + if (!self.window) { + [self clearImage]; + } +} + - (void)reloadImage { [self cancelImageLoad]; @@ -294,24 +316,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init) { [super didMoveToWindow]; - if (!self.window) { - // Don't keep self alive through the asynchronous dispatch, if the intention - // was to remove the view so it would deallocate. - __weak typeof(self) weakSelf = self; - - dispatch_async(dispatch_get_main_queue(), ^{ - __strong typeof(self) strongSelf = weakSelf; - if (!strongSelf) { - return; - } - - // If we haven't been re-added to a window by this run loop iteration, - // clear out the image to save memory. - if (!strongSelf.window) { - [strongSelf clearImage]; - } - }); - } else if (!self.image || self.image == _defaultImage) { + if (self.window && (!self.image || self.image == _defaultImage)) { [self reloadImage]; } }