RCTImageView.mm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import <React/RCTImageView.h>
  8. #import <React/RCTBridge.h>
  9. #import <React/RCTConvert.h>
  10. #import <React/RCTEventDispatcher.h>
  11. #import <React/RCTImageBlurUtils.h>
  12. #import <React/RCTImageSource.h>
  13. #import <React/RCTImageUtils.h>
  14. #import <React/RCTImageLoaderWithAttributionProtocol.h>
  15. #import <React/RCTUIImageViewAnimated.h>
  16. #import <React/RCTUtils.h>
  17. #import <React/UIView+React.h>
  18. /**
  19. * Determines whether an image of `currentSize` should be reloaded for display
  20. * at `idealSize`.
  21. */
  22. static BOOL RCTShouldReloadImageForSizeChange(CGSize currentSize, CGSize idealSize)
  23. {
  24. static const CGFloat upscaleThreshold = 1.2;
  25. static const CGFloat downscaleThreshold = 0.5;
  26. CGFloat widthMultiplier = idealSize.width / currentSize.width;
  27. CGFloat heightMultiplier = idealSize.height / currentSize.height;
  28. return widthMultiplier > upscaleThreshold || widthMultiplier < downscaleThreshold ||
  29. heightMultiplier > upscaleThreshold || heightMultiplier < downscaleThreshold;
  30. }
  31. /**
  32. * See RCTConvert (ImageSource). We want to send down the source as a similar
  33. * JSON parameter.
  34. */
  35. static NSDictionary *onLoadParamsForSource(RCTImageSource *source)
  36. {
  37. NSDictionary *dict = @{
  38. @"width": @(source.size.width),
  39. @"height": @(source.size.height),
  40. @"url": source.request.URL.absoluteString,
  41. };
  42. return @{ @"source": dict };
  43. }
  44. @interface RCTImageView ()
  45. @property (nonatomic, copy) RCTDirectEventBlock onLoadStart;
  46. @property (nonatomic, copy) RCTDirectEventBlock onProgress;
  47. @property (nonatomic, copy) RCTDirectEventBlock onError;
  48. @property (nonatomic, copy) RCTDirectEventBlock onPartialLoad;
  49. @property (nonatomic, copy) RCTDirectEventBlock onLoad;
  50. @property (nonatomic, copy) RCTDirectEventBlock onLoadEnd;
  51. @end
  52. @implementation RCTImageView
  53. {
  54. // Weak reference back to the bridge, for image loading
  55. __weak RCTBridge *_bridge;
  56. // The image source that's currently displayed
  57. RCTImageSource *_imageSource;
  58. // The image source that's being loaded from the network
  59. RCTImageSource *_pendingImageSource;
  60. // Size of the image loaded / being loaded, so we can determine when to issue a reload to accommodate a changing size.
  61. CGSize _targetSize;
  62. // A block that can be invoked to cancel the most recent call to -reloadImage, if any
  63. RCTImageLoaderCancellationBlock _reloadImageCancellationBlock;
  64. // Whether the latest change of props requires the image to be reloaded
  65. BOOL _needsReload;
  66. RCTUIImageViewAnimated *_imageView;
  67. }
  68. - (instancetype)initWithBridge:(RCTBridge *)bridge
  69. {
  70. if ((self = [super initWithFrame:CGRectZero])) {
  71. _bridge = bridge;
  72. _imageView = [[RCTUIImageViewAnimated alloc] init];
  73. _imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  74. [self addSubview:_imageView];
  75. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  76. [center addObserver:self
  77. selector:@selector(clearImageIfDetached)
  78. name:UIApplicationDidReceiveMemoryWarningNotification
  79. object:nil];
  80. [center addObserver:self
  81. selector:@selector(clearImageIfDetached)
  82. name:UIApplicationDidEnterBackgroundNotification
  83. object:nil];
  84. #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  85. if (@available(iOS 13.0, *)) {
  86. [center addObserver:self
  87. selector:@selector(clearImageIfDetached)
  88. name:UISceneDidEnterBackgroundNotification
  89. object:nil];
  90. }
  91. #endif
  92. }
  93. return self;
  94. }
  95. RCT_NOT_IMPLEMENTED(- (instancetype)init)
  96. RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
  97. RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame)
  98. - (void)updateWithImage:(UIImage *)image
  99. {
  100. if (!image) {
  101. _imageView.image = nil;
  102. return;
  103. }
  104. // Apply rendering mode
  105. if (_renderingMode != image.renderingMode) {
  106. image = [image imageWithRenderingMode:_renderingMode];
  107. }
  108. if (_resizeMode == RCTResizeModeRepeat) {
  109. image = [image resizableImageWithCapInsets:_capInsets resizingMode:UIImageResizingModeTile];
  110. } else if (!UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, _capInsets)) {
  111. // Applying capInsets of 0 will switch the "resizingMode" of the image to "tile" which is undesired
  112. image = [image resizableImageWithCapInsets:_capInsets resizingMode:UIImageResizingModeStretch];
  113. }
  114. // Apply trilinear filtering to smooth out mis-sized images
  115. _imageView.layer.minificationFilter = kCAFilterTrilinear;
  116. _imageView.layer.magnificationFilter = kCAFilterTrilinear;
  117. _imageView.image = image;
  118. }
  119. - (void)setImage:(UIImage *)image
  120. {
  121. image = image ?: _defaultImage;
  122. if (image != self.image) {
  123. [self updateWithImage:image];
  124. }
  125. }
  126. - (UIImage *)image {
  127. return _imageView.image;
  128. }
  129. - (void)setBlurRadius:(CGFloat)blurRadius
  130. {
  131. if (blurRadius != _blurRadius) {
  132. _blurRadius = blurRadius;
  133. _needsReload = YES;
  134. }
  135. }
  136. - (void)setCapInsets:(UIEdgeInsets)capInsets
  137. {
  138. if (!UIEdgeInsetsEqualToEdgeInsets(_capInsets, capInsets)) {
  139. if (UIEdgeInsetsEqualToEdgeInsets(_capInsets, UIEdgeInsetsZero) ||
  140. UIEdgeInsetsEqualToEdgeInsets(capInsets, UIEdgeInsetsZero)) {
  141. _capInsets = capInsets;
  142. // Need to reload image when enabling or disabling capInsets
  143. _needsReload = YES;
  144. } else {
  145. _capInsets = capInsets;
  146. [self updateWithImage:self.image];
  147. }
  148. }
  149. }
  150. - (void)setRenderingMode:(UIImageRenderingMode)renderingMode
  151. {
  152. if (_renderingMode != renderingMode) {
  153. _renderingMode = renderingMode;
  154. [self updateWithImage:self.image];
  155. }
  156. }
  157. - (void)setImageSources:(NSArray<RCTImageSource *> *)imageSources
  158. {
  159. if (![imageSources isEqual:_imageSources]) {
  160. _imageSources = [imageSources copy];
  161. _needsReload = YES;
  162. }
  163. }
  164. - (void)setResizeMode:(RCTResizeMode)resizeMode
  165. {
  166. if (_resizeMode != resizeMode) {
  167. _resizeMode = resizeMode;
  168. if (_resizeMode == RCTResizeModeRepeat) {
  169. // Repeat resize mode is handled by the UIImage. Use scale to fill
  170. // so the repeated image fills the UIImageView.
  171. _imageView.contentMode = UIViewContentModeScaleToFill;
  172. } else {
  173. _imageView.contentMode = (UIViewContentMode)resizeMode;
  174. }
  175. if ([self shouldReloadImageSourceAfterResize]) {
  176. _needsReload = YES;
  177. }
  178. }
  179. }
  180. - (void)cancelImageLoad
  181. {
  182. RCTImageLoaderCancellationBlock previousCancellationBlock = _reloadImageCancellationBlock;
  183. if (previousCancellationBlock) {
  184. previousCancellationBlock();
  185. _reloadImageCancellationBlock = nil;
  186. }
  187. _pendingImageSource = nil;
  188. }
  189. - (void)clearImage
  190. {
  191. [self cancelImageLoad];
  192. self.image = nil;
  193. _imageSource = nil;
  194. }
  195. - (void)clearImageIfDetached
  196. {
  197. if (!self.window) {
  198. [self clearImage];
  199. }
  200. }
  201. - (BOOL)hasMultipleSources
  202. {
  203. return _imageSources.count > 1;
  204. }
  205. - (RCTImageSource *)imageSourceForSize:(CGSize)size
  206. {
  207. if (![self hasMultipleSources]) {
  208. return _imageSources.firstObject;
  209. }
  210. // Need to wait for layout pass before deciding.
  211. if (CGSizeEqualToSize(size, CGSizeZero)) {
  212. return nil;
  213. }
  214. const CGFloat scale = RCTScreenScale();
  215. const CGFloat targetImagePixels = size.width * size.height * scale * scale;
  216. RCTImageSource *bestSource = nil;
  217. CGFloat bestFit = CGFLOAT_MAX;
  218. for (RCTImageSource *source in _imageSources) {
  219. CGSize imgSize = source.size;
  220. const CGFloat imagePixels =
  221. imgSize.width * imgSize.height * source.scale * source.scale;
  222. const CGFloat fit = ABS(1 - (imagePixels / targetImagePixels));
  223. if (fit < bestFit) {
  224. bestFit = fit;
  225. bestSource = source;
  226. }
  227. }
  228. return bestSource;
  229. }
  230. - (BOOL)shouldReloadImageSourceAfterResize
  231. {
  232. // If capInsets are set, image doesn't need reloading when resized
  233. return UIEdgeInsetsEqualToEdgeInsets(_capInsets, UIEdgeInsetsZero);
  234. }
  235. - (BOOL)shouldChangeImageSource
  236. {
  237. // We need to reload if the desired image source is different from the current image
  238. // source AND the image load that's pending
  239. RCTImageSource *desiredImageSource = [self imageSourceForSize:self.frame.size];
  240. return ![desiredImageSource isEqual:_imageSource] &&
  241. ![desiredImageSource isEqual:_pendingImageSource];
  242. }
  243. - (void)reloadImage
  244. {
  245. [self cancelImageLoad];
  246. _needsReload = NO;
  247. RCTImageSource *source = [self imageSourceForSize:self.frame.size];
  248. _pendingImageSource = source;
  249. if (source && self.frame.size.width > 0 && self.frame.size.height > 0) {
  250. if (_onLoadStart) {
  251. _onLoadStart(nil);
  252. }
  253. RCTImageLoaderProgressBlock progressHandler = nil;
  254. if (_onProgress) {
  255. progressHandler = ^(int64_t loaded, int64_t total) {
  256. self->_onProgress(@{
  257. @"loaded": @((double)loaded),
  258. @"total": @((double)total),
  259. });
  260. };
  261. }
  262. __weak RCTImageView *weakSelf = self;
  263. RCTImageLoaderPartialLoadBlock partialLoadHandler = ^(UIImage *image) {
  264. [weakSelf imageLoaderLoadedImage:image error:nil forImageSource:source partial:YES];
  265. };
  266. CGSize imageSize = self.bounds.size;
  267. CGFloat imageScale = RCTScreenScale();
  268. if (!UIEdgeInsetsEqualToEdgeInsets(_capInsets, UIEdgeInsetsZero)) {
  269. // Don't resize images that use capInsets
  270. imageSize = CGSizeZero;
  271. imageScale = source.scale;
  272. }
  273. RCTImageLoaderCompletionBlock completionHandler = ^(NSError *error, UIImage *loadedImage) {
  274. [weakSelf imageLoaderLoadedImage:loadedImage error:error forImageSource:source partial:NO];
  275. };
  276. id<RCTImageLoaderWithAttributionProtocol> imageLoader = [_bridge moduleForName:@"ImageLoader"
  277. lazilyLoadIfNecessary:YES];
  278. RCTImageURLLoaderRequest *loaderRequest = [imageLoader loadImageWithURLRequest:source.request
  279. size:imageSize
  280. scale:imageScale
  281. clipped:NO
  282. resizeMode:_resizeMode
  283. priority:RCTImageLoaderPriorityImmediate
  284. attribution:{
  285. .nativeViewTag = [self.reactTag intValue],
  286. .surfaceId = [self.rootTag intValue],
  287. }
  288. progressBlock:progressHandler
  289. partialLoadBlock:partialLoadHandler
  290. completionBlock:completionHandler];
  291. _reloadImageCancellationBlock = loaderRequest.cancellationBlock;
  292. } else {
  293. [self clearImage];
  294. }
  295. }
  296. - (void)imageLoaderLoadedImage:(UIImage *)loadedImage error:(NSError *)error forImageSource:(RCTImageSource *)source partial:(BOOL)isPartialLoad
  297. {
  298. if (![source isEqual:_pendingImageSource]) {
  299. // Bail out if source has changed since we started loading
  300. return;
  301. }
  302. if (error) {
  303. RCTExecuteOnMainQueue(^{
  304. self.image = nil;
  305. });
  306. if (_onError) {
  307. _onError(@{ @"error": error.localizedDescription });
  308. }
  309. if (_onLoadEnd) {
  310. _onLoadEnd(nil);
  311. }
  312. return;
  313. }
  314. void (^setImageBlock)(UIImage *) = ^(UIImage *image) {
  315. if (!isPartialLoad) {
  316. self->_imageSource = source;
  317. self->_pendingImageSource = nil;
  318. }
  319. self.image = image;
  320. if (isPartialLoad) {
  321. if (self->_onPartialLoad) {
  322. self->_onPartialLoad(nil);
  323. }
  324. } else {
  325. if (self->_onLoad) {
  326. RCTImageSource *sourceLoaded = [source imageSourceWithSize:image.size scale:source.scale];
  327. self->_onLoad(onLoadParamsForSource(sourceLoaded));
  328. }
  329. if (self->_onLoadEnd) {
  330. self->_onLoadEnd(nil);
  331. }
  332. }
  333. };
  334. if (_blurRadius > __FLT_EPSILON__) {
  335. // Blur on a background thread to avoid blocking interaction
  336. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  337. UIImage *blurredImage = RCTBlurredImageWithRadius(loadedImage, self->_blurRadius);
  338. RCTExecuteOnMainQueue(^{
  339. setImageBlock(blurredImage);
  340. });
  341. });
  342. } else {
  343. // No blur, so try to set the image on the main thread synchronously to minimize image
  344. // flashing. (For instance, if this view gets attached to a window, then -didMoveToWindow
  345. // calls -reloadImage, and we want to set the image synchronously if possible so that the
  346. // image property is set in the same CATransaction that attaches this view to the window.)
  347. RCTExecuteOnMainQueue(^{
  348. setImageBlock(loadedImage);
  349. });
  350. }
  351. }
  352. - (void)reactSetFrame:(CGRect)frame
  353. {
  354. [super reactSetFrame:frame];
  355. // If we didn't load an image yet, or the new frame triggers a different image source
  356. // to be loaded, reload to swap to the proper image source.
  357. if ([self shouldChangeImageSource]) {
  358. _targetSize = frame.size;
  359. [self reloadImage];
  360. } else if ([self shouldReloadImageSourceAfterResize]) {
  361. CGSize imageSize = self.image.size;
  362. CGFloat imageScale = self.image.scale;
  363. CGSize idealSize = RCTTargetSize(imageSize, imageScale, frame.size, RCTScreenScale(),
  364. (RCTResizeMode)self.contentMode, YES);
  365. // Don't reload if the current image or target image size is close enough
  366. if (!RCTShouldReloadImageForSizeChange(imageSize, idealSize) ||
  367. !RCTShouldReloadImageForSizeChange(_targetSize, idealSize)) {
  368. return;
  369. }
  370. // Don't reload if the current image size is the maximum size of either the pending image source or image source
  371. CGSize imageSourceSize = (_imageSource ? _imageSource : _pendingImageSource).size;
  372. if (imageSize.width * imageScale == imageSourceSize.width * _imageSource.scale &&
  373. imageSize.height * imageScale == imageSourceSize.height * _imageSource.scale) {
  374. return;
  375. }
  376. RCTLogInfo(@"Reloading image %@ as size %@", _imageSource.request.URL.absoluteString, NSStringFromCGSize(idealSize));
  377. // If the existing image or an image being loaded are not the right
  378. // size, reload the asset in case there is a better size available.
  379. _targetSize = idealSize;
  380. [self reloadImage];
  381. }
  382. }
  383. - (void)didSetProps:(NSArray<NSString *> *)changedProps
  384. {
  385. if (_needsReload) {
  386. [self reloadImage];
  387. }
  388. }
  389. - (void)didMoveToWindow
  390. {
  391. [super didMoveToWindow];
  392. if (!self.window) {
  393. // Cancel loading the image if we've moved offscreen. In addition to helping
  394. // prioritise image requests that are actually on-screen, this removes
  395. // requests that have gotten "stuck" from the queue, unblocking other images
  396. // from loading.
  397. [self cancelImageLoad];
  398. } else if ([self shouldChangeImageSource]) {
  399. [self reloadImage];
  400. }
  401. }
  402. @end