2015-03-23 22:07:33 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2015-07-15 20:17:13 +00:00
|
|
|
#import "RCTImageView.h"
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2016-11-23 15:47:52 +00:00
|
|
|
#import <React/RCTBridge.h>
|
|
|
|
#import <React/RCTConvert.h>
|
|
|
|
#import <React/RCTEventDispatcher.h>
|
|
|
|
#import <React/RCTImageSource.h>
|
|
|
|
#import <React/RCTUtils.h>
|
|
|
|
#import <React/UIView+React.h>
|
|
|
|
|
|
|
|
#import "RCTImageBlurUtils.h"
|
2015-07-14 11:06:17 +00:00
|
|
|
#import "RCTImageLoader.h"
|
2015-08-12 14:07:24 +00:00
|
|
|
#import "RCTImageUtils.h"
|
2015-07-14 11:06:17 +00:00
|
|
|
|
2015-09-23 14:30:37 +00:00
|
|
|
/**
|
|
|
|
* Determines whether an image of `currentSize` should be reloaded for display
|
|
|
|
* at `idealSize`.
|
|
|
|
*/
|
2015-10-08 18:32:11 +00:00
|
|
|
static BOOL RCTShouldReloadImageForSizeChange(CGSize currentSize, CGSize idealSize)
|
|
|
|
{
|
2015-09-23 14:30:37 +00:00
|
|
|
static const CGFloat upscaleThreshold = 1.2;
|
|
|
|
static const CGFloat downscaleThreshold = 0.5;
|
|
|
|
|
|
|
|
CGFloat widthMultiplier = idealSize.width / currentSize.width;
|
|
|
|
CGFloat heightMultiplier = idealSize.height / currentSize.height;
|
|
|
|
|
|
|
|
return widthMultiplier > upscaleThreshold || widthMultiplier < downscaleThreshold ||
|
|
|
|
heightMultiplier > upscaleThreshold || heightMultiplier < downscaleThreshold;
|
|
|
|
}
|
|
|
|
|
2016-08-22 17:55:20 +00:00
|
|
|
/**
|
|
|
|
* See RCTConvert (ImageSource). We want to send down the source as a similar
|
|
|
|
* JSON parameter.
|
|
|
|
*/
|
|
|
|
static NSDictionary *onLoadParamsForSource(RCTImageSource *source)
|
|
|
|
{
|
|
|
|
NSDictionary *dict = @{
|
|
|
|
@"width": @(source.size.width),
|
|
|
|
@"height": @(source.size.height),
|
|
|
|
@"url": source.request.URL.absoluteString,
|
|
|
|
};
|
|
|
|
return @{ @"source": dict };
|
|
|
|
}
|
|
|
|
|
2015-07-15 20:17:13 +00:00
|
|
|
@interface RCTImageView ()
|
|
|
|
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
@property (nonatomic, copy) RCTDirectEventBlock onLoadStart;
|
|
|
|
@property (nonatomic, copy) RCTDirectEventBlock onProgress;
|
|
|
|
@property (nonatomic, copy) RCTDirectEventBlock onError;
|
2016-09-21 19:11:19 +00:00
|
|
|
@property (nonatomic, copy) RCTDirectEventBlock onPartialLoad;
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
@property (nonatomic, copy) RCTDirectEventBlock onLoad;
|
|
|
|
@property (nonatomic, copy) RCTDirectEventBlock onLoadEnd;
|
2015-07-15 20:17:13 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTImageView
|
|
|
|
{
|
2017-08-29 11:12:29 +00:00
|
|
|
// Weak reference back to the bridge, for image loading
|
2015-11-17 17:12:07 +00:00
|
|
|
__weak RCTBridge *_bridge;
|
2016-08-26 20:49:21 +00:00
|
|
|
|
|
|
|
// The image source that's currently displayed
|
|
|
|
RCTImageSource *_imageSource;
|
|
|
|
|
|
|
|
// The image source that's being loaded from the network
|
|
|
|
RCTImageSource *_pendingImageSource;
|
|
|
|
|
2017-08-29 11:12:29 +00:00
|
|
|
// Size of the image loaded / being loaded, so we can determine when to issue a reload to accomodate a changing size.
|
2015-09-07 00:06:35 +00:00
|
|
|
CGSize _targetSize;
|
2015-09-23 14:30:36 +00:00
|
|
|
|
2017-08-29 11:12:29 +00:00
|
|
|
// A block that can be invoked to cancel the most recent call to -reloadImage, if any
|
2015-09-23 14:30:36 +00:00
|
|
|
RCTImageLoaderCancellationBlock _reloadImageCancellationBlock;
|
2017-08-29 11:12:29 +00:00
|
|
|
|
|
|
|
// Whether the latest change of props requires the image to be reloaded
|
|
|
|
BOOL _needsReload;
|
2015-07-15 20:17:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithBridge:(RCTBridge *)bridge
|
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
|
|
|
_bridge = bridge;
|
2016-05-24 15:39:40 +00:00
|
|
|
|
|
|
|
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
|
|
|
|
[center addObserver:self
|
|
|
|
selector:@selector(clearImageIfDetached)
|
|
|
|
name:UIApplicationDidReceiveMemoryWarningNotification
|
|
|
|
object:nil];
|
|
|
|
[center addObserver:self
|
|
|
|
selector:@selector(clearImageIfDetached)
|
|
|
|
name:UIApplicationDidEnterBackgroundNotification
|
|
|
|
object:nil];
|
2015-07-15 20:17:13 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-05-24 15:39:40 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
2015-08-24 10:14:33 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2016-08-26 20:49:21 +00:00
|
|
|
- (void)updateWithImage:(UIImage *)image
|
2015-01-30 01:10:49 +00:00
|
|
|
{
|
|
|
|
if (!image) {
|
2016-08-29 18:18:24 +00:00
|
|
|
super.image = nil;
|
2015-01-30 01:10:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-03-23 22:07:33 +00:00
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
// Apply rendering mode
|
|
|
|
if (_renderingMode != image.renderingMode) {
|
|
|
|
image = [image imageWithRenderingMode:_renderingMode];
|
|
|
|
}
|
2015-03-23 22:07:33 +00:00
|
|
|
|
2016-06-22 11:13:22 +00:00
|
|
|
if (_resizeMode == RCTResizeModeRepeat) {
|
|
|
|
image = [image resizableImageWithCapInsets:_capInsets resizingMode:UIImageResizingModeTile];
|
|
|
|
} else if (!UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, _capInsets)) {
|
|
|
|
// Applying capInsets of 0 will switch the "resizingMode" of the image to "tile" which is undesired
|
2015-01-30 01:10:49 +00:00
|
|
|
image = [image resizableImageWithCapInsets:_capInsets resizingMode:UIImageResizingModeStretch];
|
|
|
|
}
|
2016-08-26 20:49:21 +00:00
|
|
|
|
2015-03-02 18:42:31 +00:00
|
|
|
// Apply trilinear filtering to smooth out mis-sized images
|
|
|
|
self.layer.minificationFilter = kCAFilterTrilinear;
|
|
|
|
self.layer.magnificationFilter = kCAFilterTrilinear;
|
2015-03-11 02:11:28 +00:00
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
super.image = image;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setImage:(UIImage *)image
|
|
|
|
{
|
2015-08-11 11:29:55 +00:00
|
|
|
image = image ?: _defaultImage;
|
2016-08-26 20:49:21 +00:00
|
|
|
if (image != self.image) {
|
|
|
|
[self updateWithImage:image];
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 19:25:07 +00:00
|
|
|
- (void)setBlurRadius:(CGFloat)blurRadius
|
|
|
|
{
|
|
|
|
if (blurRadius != _blurRadius) {
|
|
|
|
_blurRadius = blurRadius;
|
2017-08-29 11:12:29 +00:00
|
|
|
_needsReload = YES;
|
2016-03-17 19:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
- (void)setCapInsets:(UIEdgeInsets)capInsets
|
|
|
|
{
|
|
|
|
if (!UIEdgeInsetsEqualToEdgeInsets(_capInsets, capInsets)) {
|
2016-01-20 19:03:22 +00:00
|
|
|
if (UIEdgeInsetsEqualToEdgeInsets(_capInsets, UIEdgeInsetsZero) ||
|
|
|
|
UIEdgeInsetsEqualToEdgeInsets(capInsets, UIEdgeInsetsZero)) {
|
|
|
|
_capInsets = capInsets;
|
|
|
|
// Need to reload image when enabling or disabling capInsets
|
2017-08-29 11:12:29 +00:00
|
|
|
_needsReload = YES;
|
2016-01-20 19:03:22 +00:00
|
|
|
} else {
|
|
|
|
_capInsets = capInsets;
|
2016-08-26 20:49:21 +00:00
|
|
|
[self updateWithImage:self.image];
|
2016-01-20 19:03:22 +00:00
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setRenderingMode:(UIImageRenderingMode)renderingMode
|
|
|
|
{
|
|
|
|
if (_renderingMode != renderingMode) {
|
|
|
|
_renderingMode = renderingMode;
|
2016-08-26 20:49:21 +00:00
|
|
|
[self updateWithImage:self.image];
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-23 18:26:35 +00:00
|
|
|
- (void)setImageSources:(NSArray<RCTImageSource *> *)imageSources
|
2015-07-14 11:06:17 +00:00
|
|
|
{
|
2016-08-23 18:26:35 +00:00
|
|
|
if (![imageSources isEqual:_imageSources]) {
|
|
|
|
_imageSources = [imageSources copy];
|
2017-08-29 11:12:29 +00:00
|
|
|
_needsReload = YES;
|
2015-07-14 11:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-22 11:13:22 +00:00
|
|
|
- (void)setResizeMode:(RCTResizeMode)resizeMode
|
2015-08-12 14:07:24 +00:00
|
|
|
{
|
2016-06-22 11:13:22 +00:00
|
|
|
if (_resizeMode != resizeMode) {
|
|
|
|
_resizeMode = resizeMode;
|
|
|
|
|
|
|
|
if (_resizeMode == RCTResizeModeRepeat) {
|
|
|
|
// Repeat resize mode is handled by the UIImage. Use scale to fill
|
|
|
|
// so the repeated image fills the UIImageView.
|
|
|
|
self.contentMode = UIViewContentModeScaleToFill;
|
|
|
|
} else {
|
|
|
|
self.contentMode = (UIViewContentMode)resizeMode;
|
|
|
|
}
|
|
|
|
|
2016-08-26 20:49:21 +00:00
|
|
|
if ([self shouldReloadImageSourceAfterResize]) {
|
2017-08-29 11:12:29 +00:00
|
|
|
_needsReload = YES;
|
2015-08-12 14:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 14:30:36 +00:00
|
|
|
- (void)cancelImageLoad
|
|
|
|
{
|
|
|
|
RCTImageLoaderCancellationBlock previousCancellationBlock = _reloadImageCancellationBlock;
|
|
|
|
if (previousCancellationBlock) {
|
|
|
|
previousCancellationBlock();
|
|
|
|
_reloadImageCancellationBlock = nil;
|
|
|
|
}
|
2016-08-26 20:49:21 +00:00
|
|
|
|
|
|
|
_pendingImageSource = nil;
|
2015-09-23 14:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)clearImage
|
|
|
|
{
|
|
|
|
[self cancelImageLoad];
|
|
|
|
[self.layer removeAnimationForKey:@"contents"];
|
|
|
|
self.image = nil;
|
2016-09-06 16:20:13 +00:00
|
|
|
_imageSource = nil;
|
2015-09-23 14:30:36 +00:00
|
|
|
}
|
|
|
|
|
2016-05-24 15:39:40 +00:00
|
|
|
- (void)clearImageIfDetached
|
|
|
|
{
|
|
|
|
if (!self.window) {
|
|
|
|
[self clearImage];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:58:50 +00:00
|
|
|
- (BOOL)hasMultipleSources
|
|
|
|
{
|
2016-08-23 18:26:35 +00:00
|
|
|
return _imageSources.count > 1;
|
2016-07-28 20:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (RCTImageSource *)imageSourceForSize:(CGSize)size
|
|
|
|
{
|
2017-10-25 15:04:36 +00:00
|
|
|
if (![self hasMultipleSources]) {
|
2016-08-23 18:26:35 +00:00
|
|
|
return _imageSources.firstObject;
|
2016-07-28 20:58:50 +00:00
|
|
|
}
|
2016-08-26 20:49:21 +00:00
|
|
|
|
2017-10-25 15:04:36 +00:00
|
|
|
// Need to wait for layout pass before deciding.
|
2016-07-28 20:58:50 +00:00
|
|
|
if (CGSizeEqualToSize(size, CGSizeZero)) {
|
|
|
|
return nil;
|
|
|
|
}
|
2016-08-26 20:49:23 +00:00
|
|
|
|
2016-07-28 20:58:50 +00:00
|
|
|
const CGFloat scale = RCTScreenScale();
|
|
|
|
const CGFloat targetImagePixels = size.width * size.height * scale * scale;
|
|
|
|
|
|
|
|
RCTImageSource *bestSource = nil;
|
|
|
|
CGFloat bestFit = CGFLOAT_MAX;
|
2016-08-23 18:26:35 +00:00
|
|
|
for (RCTImageSource *source in _imageSources) {
|
2016-07-28 20:58:50 +00:00
|
|
|
CGSize imgSize = source.size;
|
|
|
|
const CGFloat imagePixels =
|
|
|
|
imgSize.width * imgSize.height * source.scale * source.scale;
|
|
|
|
const CGFloat fit = ABS(1 - (imagePixels / targetImagePixels));
|
|
|
|
|
|
|
|
if (fit < bestFit) {
|
|
|
|
bestFit = fit;
|
|
|
|
bestSource = source;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bestSource;
|
|
|
|
}
|
|
|
|
|
2016-08-26 20:49:21 +00:00
|
|
|
- (BOOL)shouldReloadImageSourceAfterResize
|
2016-07-28 20:58:50 +00:00
|
|
|
{
|
2016-08-26 20:49:21 +00:00
|
|
|
// If capInsets are set, image doesn't need reloading when resized
|
|
|
|
return UIEdgeInsetsEqualToEdgeInsets(_capInsets, UIEdgeInsetsZero);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)shouldChangeImageSource
|
|
|
|
{
|
|
|
|
// We need to reload if the desired image source is different from the current image
|
|
|
|
// source AND the image load that's pending
|
|
|
|
RCTImageSource *desiredImageSource = [self imageSourceForSize:self.frame.size];
|
|
|
|
return ![desiredImageSource isEqual:_imageSource] &&
|
|
|
|
![desiredImageSource isEqual:_pendingImageSource];
|
2016-07-28 20:58:50 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 11:06:17 +00:00
|
|
|
- (void)reloadImage
|
|
|
|
{
|
2015-09-23 14:30:36 +00:00
|
|
|
[self cancelImageLoad];
|
2017-08-29 11:12:29 +00:00
|
|
|
_needsReload = NO;
|
2015-09-23 14:30:36 +00:00
|
|
|
|
2016-08-23 18:26:35 +00:00
|
|
|
RCTImageSource *source = [self imageSourceForSize:self.frame.size];
|
2016-08-26 20:49:21 +00:00
|
|
|
_pendingImageSource = source;
|
2016-07-28 20:58:50 +00:00
|
|
|
|
2017-10-25 15:04:36 +00:00
|
|
|
if (source && self.frame.size.width > 0 && self.frame.size.height > 0) {
|
2015-07-15 20:17:13 +00:00
|
|
|
if (_onLoadStart) {
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
_onLoadStart(nil);
|
2015-07-15 20:17:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCTImageLoaderProgressBlock progressHandler = nil;
|
|
|
|
if (_onProgress) {
|
2015-07-27 20:46:59 +00:00
|
|
|
progressHandler = ^(int64_t loaded, int64_t total) {
|
2016-07-07 19:36:56 +00:00
|
|
|
self->_onProgress(@{
|
2015-07-27 20:46:59 +00:00
|
|
|
@"loaded": @((double)loaded),
|
|
|
|
@"total": @((double)total),
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
});
|
2015-07-15 20:17:13 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-21 19:11:19 +00:00
|
|
|
__weak RCTImageView *weakSelf = self;
|
|
|
|
RCTImageLoaderPartialLoadBlock partialLoadHandler = ^(UIImage *image) {
|
|
|
|
[weakSelf imageLoaderLoadedImage:image error:nil forImageSource:source partial:YES];
|
|
|
|
};
|
|
|
|
|
2016-01-20 19:03:22 +00:00
|
|
|
CGSize imageSize = self.bounds.size;
|
|
|
|
CGFloat imageScale = RCTScreenScale();
|
|
|
|
if (!UIEdgeInsetsEqualToEdgeInsets(_capInsets, UIEdgeInsetsZero)) {
|
|
|
|
// Don't resize images that use capInsets
|
|
|
|
imageSize = CGSizeZero;
|
2016-08-26 20:49:21 +00:00
|
|
|
imageScale = source.scale;
|
2016-01-20 19:03:22 +00:00
|
|
|
}
|
|
|
|
|
2016-08-19 17:31:42 +00:00
|
|
|
RCTImageLoaderCompletionBlock completionHandler = ^(NSError *error, UIImage *loadedImage) {
|
2016-09-21 19:11:19 +00:00
|
|
|
[weakSelf imageLoaderLoadedImage:loadedImage error:error forImageSource:source partial:NO];
|
2016-08-19 17:31:42 +00:00
|
|
|
};
|
|
|
|
|
2016-06-01 17:32:20 +00:00
|
|
|
_reloadImageCancellationBlock =
|
2016-07-28 20:58:50 +00:00
|
|
|
[_bridge.imageLoader loadImageWithURLRequest:source.request
|
2016-06-01 17:32:20 +00:00
|
|
|
size:imageSize
|
|
|
|
scale:imageScale
|
|
|
|
clipped:NO
|
2016-06-22 11:13:22 +00:00
|
|
|
resizeMode:_resizeMode
|
2016-06-01 17:32:20 +00:00
|
|
|
progressBlock:progressHandler
|
2016-09-21 19:11:19 +00:00
|
|
|
partialLoadBlock:partialLoadHandler
|
2016-08-19 17:31:42 +00:00
|
|
|
completionBlock:completionHandler];
|
2015-07-14 11:06:17 +00:00
|
|
|
} else {
|
2015-09-23 14:30:36 +00:00
|
|
|
[self clearImage];
|
2015-07-14 11:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 19:11:19 +00:00
|
|
|
- (void)imageLoaderLoadedImage:(UIImage *)loadedImage error:(NSError *)error forImageSource:(RCTImageSource *)source partial:(BOOL)isPartialLoad
|
2016-08-19 17:31:42 +00:00
|
|
|
{
|
2016-08-26 20:49:21 +00:00
|
|
|
if (![source isEqual:_pendingImageSource]) {
|
2016-08-19 17:31:42 +00:00
|
|
|
// Bail out if source has changed since we started loading
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
if (_onError) {
|
|
|
|
_onError(@{ @"error": error.localizedDescription });
|
|
|
|
}
|
|
|
|
if (_onLoadEnd) {
|
|
|
|
_onLoadEnd(nil);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void (^setImageBlock)(UIImage *) = ^(UIImage *image) {
|
2016-09-21 19:11:19 +00:00
|
|
|
if (!isPartialLoad) {
|
|
|
|
self->_imageSource = source;
|
|
|
|
self->_pendingImageSource = nil;
|
|
|
|
}
|
2016-08-26 20:49:21 +00:00
|
|
|
|
2016-08-19 17:31:42 +00:00
|
|
|
if (image.reactKeyframeAnimation) {
|
|
|
|
[self.layer addAnimation:image.reactKeyframeAnimation forKey:@"contents"];
|
|
|
|
} else {
|
|
|
|
[self.layer removeAnimationForKey:@"contents"];
|
|
|
|
self.image = image;
|
|
|
|
}
|
|
|
|
|
2016-09-21 19:11:19 +00:00
|
|
|
if (isPartialLoad) {
|
|
|
|
if (self->_onPartialLoad) {
|
|
|
|
self->_onPartialLoad(nil);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (self->_onLoad) {
|
2017-07-20 09:49:35 +00:00
|
|
|
RCTImageSource *sourceLoaded = [source imageSourceWithSize:image.size scale:source.scale];
|
|
|
|
self->_onLoad(onLoadParamsForSource(sourceLoaded));
|
2016-09-21 19:11:19 +00:00
|
|
|
}
|
|
|
|
if (self->_onLoadEnd) {
|
|
|
|
self->_onLoadEnd(nil);
|
|
|
|
}
|
2016-08-19 17:31:42 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (_blurRadius > __FLT_EPSILON__) {
|
|
|
|
// Blur on a background thread to avoid blocking interaction
|
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
|
|
UIImage *blurredImage = RCTBlurredImageWithRadius(loadedImage, self->_blurRadius);
|
|
|
|
RCTExecuteOnMainQueue(^{
|
|
|
|
setImageBlock(blurredImage);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// No blur, so try to set the image on the main thread synchronously to minimize image
|
|
|
|
// flashing. (For instance, if this view gets attached to a window, then -didMoveToWindow
|
|
|
|
// calls -reloadImage, and we want to set the image synchronously if possible so that the
|
|
|
|
// image property is set in the same CATransaction that attaches this view to the window.)
|
|
|
|
RCTExecuteOnMainQueue(^{
|
|
|
|
setImageBlock(loadedImage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-14 11:06:17 +00:00
|
|
|
- (void)reactSetFrame:(CGRect)frame
|
|
|
|
{
|
|
|
|
[super reactSetFrame:frame];
|
2015-10-19 17:24:01 +00:00
|
|
|
|
2016-08-26 20:49:21 +00:00
|
|
|
// If we didn't load an image yet, or the new frame triggers a different image source
|
|
|
|
// to be loaded, reload to swap to the proper image source.
|
|
|
|
if ([self shouldChangeImageSource]) {
|
2015-09-07 00:06:35 +00:00
|
|
|
_targetSize = frame.size;
|
2015-07-14 11:06:17 +00:00
|
|
|
[self reloadImage];
|
2016-08-26 20:49:21 +00:00
|
|
|
} else if ([self shouldReloadImageSourceAfterResize]) {
|
2015-09-23 14:30:37 +00:00
|
|
|
CGSize imageSize = self.image.size;
|
2016-08-26 20:49:23 +00:00
|
|
|
CGFloat imageScale = self.image.scale;
|
|
|
|
CGSize idealSize = RCTTargetSize(imageSize, imageScale, frame.size, RCTScreenScale(),
|
|
|
|
(RCTResizeMode)self.contentMode, YES);
|
|
|
|
|
|
|
|
// Don't reload if the current image or target image size is close enough
|
|
|
|
if (!RCTShouldReloadImageForSizeChange(imageSize, idealSize) ||
|
|
|
|
!RCTShouldReloadImageForSizeChange(_targetSize, idealSize)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-26 20:49:21 +00:00
|
|
|
|
2016-08-26 20:49:23 +00:00
|
|
|
// Don't reload if the current image size is the maximum size of the image source
|
|
|
|
CGSize imageSourceSize = _imageSource.size;
|
|
|
|
if (imageSize.width * imageScale == imageSourceSize.width * _imageSource.scale &&
|
|
|
|
imageSize.height * imageScale == imageSourceSize.height * _imageSource.scale) {
|
|
|
|
return;
|
2015-07-14 11:06:17 +00:00
|
|
|
}
|
2016-08-26 20:49:23 +00:00
|
|
|
|
|
|
|
RCTLogInfo(@"Reloading image %@ as size %@", _imageSource.request.URL.absoluteString, NSStringFromCGSize(idealSize));
|
|
|
|
|
|
|
|
// If the existing image or an image being loaded are not the right
|
|
|
|
// size, reload the asset in case there is a better size available.
|
|
|
|
_targetSize = idealSize;
|
|
|
|
[self reloadImage];
|
2015-07-14 11:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 11:12:29 +00:00
|
|
|
- (void)didSetProps:(NSArray<NSString *> *)changedProps
|
|
|
|
{
|
|
|
|
if (_needsReload) {
|
|
|
|
[self reloadImage];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 15:17:39 +00:00
|
|
|
- (void)didMoveToWindow
|
2015-07-21 16:10:52 +00:00
|
|
|
{
|
2015-08-04 15:17:39 +00:00
|
|
|
[super didMoveToWindow];
|
2015-07-21 16:10:52 +00:00
|
|
|
|
2016-06-09 16:55:18 +00:00
|
|
|
if (!self.window) {
|
|
|
|
// Cancel loading the image if we've moved offscreen. In addition to helping
|
2016-06-09 23:20:59 +00:00
|
|
|
// prioritise image requests that are actually on-screen, this removes
|
2016-06-09 16:55:18 +00:00
|
|
|
// requests that have gotten "stuck" from the queue, unblocking other images
|
|
|
|
// from loading.
|
|
|
|
[self cancelImageLoad];
|
2016-08-26 20:49:21 +00:00
|
|
|
} else if ([self shouldChangeImageSource]) {
|
2015-07-21 16:10:52 +00:00
|
|
|
[self reloadImage];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
@end
|