From 189c2c8958442541c6b4f42860b2943ece612da2 Mon Sep 17 00:00:00 2001 From: Mo Wang Date: Fri, 25 Feb 2022 19:38:18 -0800 Subject: [PATCH] Synchronously render cached images Summary: ## Problem Previously the RN Image render the cached images asynchronously (line 555 and 594 prior to the change), which caused the images to render at least a frame later than the adjacent components. ## Change In this change, we call partialLoadHandler with the cached image synchronously on the main thread. Changelog: [iOS][Changed] - Synchronously render cached images Reviewed By: p-sun Differential Revision: D34487673 fbshipit-source-id: 0600c2fa5f7a1eca71b8582bbe968694cf211468 --- Libraries/Image/RCTImageLoader.mm | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Libraries/Image/RCTImageLoader.mm b/Libraries/Image/RCTImageLoader.mm index 8b2bfd1afc04c3..54b4c634850ce6 100644 --- a/Libraries/Image/RCTImageLoader.mm +++ b/Libraries/Image/RCTImageLoader.mm @@ -492,6 +492,16 @@ - (RCTImageURLLoaderRequest *)_loadImageOrDataWithURLRequest:(NSURLRequest *)req BOOL cacheResult = [loadHandler respondsToSelector:@selector(shouldCacheLoadedImages)] ? [loadHandler shouldCacheLoadedImages] : YES; + if (cacheResult) { + UIImage *image = [[self imageCache] imageForUrl:request.URL.absoluteString + size:size + scale:scale + resizeMode:resizeMode]; + if (image) { + partialLoadHandler(image); + } + } + auto cancelled = std::make_shared>(0); __block dispatch_block_t cancelLoad = nil; __block NSLock *cancelLoadLock = [NSLock new];