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
|
|
|
|
2015-07-15 20:17:13 +00:00
|
|
|
#import "RCTBridge.h"
|
2015-07-14 11:06:17 +00:00
|
|
|
#import "RCTConvert.h"
|
2015-07-15 20:17:13 +00:00
|
|
|
#import "RCTEventDispatcher.h"
|
2015-07-14 11:06:17 +00:00
|
|
|
#import "RCTImageLoader.h"
|
2015-12-08 11:29:08 +00:00
|
|
|
#import "RCTImageSource.h"
|
2015-08-12 14:07:24 +00:00
|
|
|
#import "RCTImageUtils.h"
|
2015-07-14 11:06:17 +00:00
|
|
|
#import "RCTUtils.h"
|
2016-03-17 19:25:07 +00:00
|
|
|
#import "RCTImageBlurUtils.h"
|
2015-07-14 11:06:17 +00:00
|
|
|
|
|
|
|
#import "UIView+React.h"
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
@property (nonatomic, copy) RCTDirectEventBlock onLoad;
|
|
|
|
@property (nonatomic, copy) RCTDirectEventBlock onLoadEnd;
|
2015-07-15 20:17:13 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTImageView
|
|
|
|
{
|
2015-11-17 17:12:07 +00:00
|
|
|
__weak RCTBridge *_bridge;
|
2015-09-07 00:06:35 +00:00
|
|
|
CGSize _targetSize;
|
2015-09-23 14:30:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A block that can be invoked to cancel the most recent call to -reloadImage,
|
|
|
|
* if any.
|
|
|
|
*/
|
|
|
|
RCTImageLoaderCancellationBlock _reloadImageCancellationBlock;
|
2015-07-15 20:17:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithBridge:(RCTBridge *)bridge
|
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
|
|
|
_bridge = bridge;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-08-24 10:14:33 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2015-08-12 14:07:24 +00:00
|
|
|
- (void)updateImage
|
2015-01-30 01:10:49 +00:00
|
|
|
{
|
|
|
|
UIImage *image = self.image;
|
|
|
|
if (!image) {
|
|
|
|
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
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
// Applying capInsets of 0 will switch the "resizingMode" of the image to "tile" which is undesired
|
|
|
|
if (!UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, _capInsets)) {
|
|
|
|
image = [image resizableImageWithCapInsets:_capInsets resizingMode:UIImageResizingModeStretch];
|
|
|
|
}
|
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;
|
2015-01-30 01:10:49 +00:00
|
|
|
if (image != super.image) {
|
2015-08-11 11:29:55 +00:00
|
|
|
super.image = image;
|
2015-08-12 14:07:24 +00:00
|
|
|
[self updateImage];
|
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;
|
|
|
|
[self reloadImage];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
[self reloadImage];
|
|
|
|
} else {
|
|
|
|
_capInsets = capInsets;
|
|
|
|
[self updateImage];
|
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setRenderingMode:(UIImageRenderingMode)renderingMode
|
|
|
|
{
|
|
|
|
if (_renderingMode != renderingMode) {
|
|
|
|
_renderingMode = renderingMode;
|
2015-08-12 14:07:24 +00:00
|
|
|
[self updateImage];
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-08 11:29:08 +00:00
|
|
|
- (void)setSource:(RCTImageSource *)source
|
2015-07-14 11:06:17 +00:00
|
|
|
{
|
2015-12-08 11:29:08 +00:00
|
|
|
if (![source isEqual:_source]) {
|
|
|
|
_source = source;
|
2015-07-14 11:06:17 +00:00
|
|
|
[self reloadImage];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-08 11:29:08 +00:00
|
|
|
- (BOOL)sourceNeedsReload
|
2015-09-02 15:25:10 +00:00
|
|
|
{
|
2016-01-20 19:03:22 +00:00
|
|
|
// If capInsets are set, image doesn't need reloading when resized
|
|
|
|
return UIEdgeInsetsEqualToEdgeInsets(_capInsets, UIEdgeInsetsZero);
|
2015-09-02 15:25:10 +00:00
|
|
|
}
|
|
|
|
|
2015-08-12 14:07:24 +00:00
|
|
|
- (void)setContentMode:(UIViewContentMode)contentMode
|
|
|
|
{
|
|
|
|
if (self.contentMode != contentMode) {
|
|
|
|
super.contentMode = contentMode;
|
2015-12-08 11:29:08 +00:00
|
|
|
if ([self sourceNeedsReload]) {
|
2015-08-12 14:07:24 +00:00
|
|
|
[self reloadImage];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 14:30:36 +00:00
|
|
|
- (void)cancelImageLoad
|
|
|
|
{
|
|
|
|
RCTImageLoaderCancellationBlock previousCancellationBlock = _reloadImageCancellationBlock;
|
|
|
|
if (previousCancellationBlock) {
|
|
|
|
previousCancellationBlock();
|
|
|
|
_reloadImageCancellationBlock = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)clearImage
|
|
|
|
{
|
|
|
|
[self cancelImageLoad];
|
|
|
|
[self.layer removeAnimationForKey:@"contents"];
|
|
|
|
self.image = nil;
|
|
|
|
}
|
|
|
|
|
2015-07-14 11:06:17 +00:00
|
|
|
- (void)reloadImage
|
|
|
|
{
|
2015-09-23 14:30:36 +00:00
|
|
|
[self cancelImageLoad];
|
|
|
|
|
2015-12-08 11:29:08 +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) {
|
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
|
|
|
_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-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;
|
|
|
|
imageScale = _source.scale;
|
|
|
|
}
|
|
|
|
|
2015-12-08 11:29:08 +00:00
|
|
|
RCTImageSource *source = _source;
|
2016-03-17 19:25:07 +00:00
|
|
|
CGFloat blurRadius = _blurRadius;
|
2015-12-08 11:29:08 +00:00
|
|
|
__weak RCTImageView *weakSelf = self;
|
2016-01-20 19:03:22 +00:00
|
|
|
_reloadImageCancellationBlock = [_bridge.imageLoader loadImageWithoutClipping:_source.imageURL.absoluteString
|
|
|
|
size:imageSize
|
|
|
|
scale:imageScale
|
|
|
|
resizeMode:(RCTResizeMode)self.contentMode
|
|
|
|
progressBlock:progressHandler
|
|
|
|
completionBlock:^(NSError *error, UIImage *image) {
|
2016-03-17 19:25:07 +00:00
|
|
|
RCTImageView *strongSelf = weakSelf;
|
|
|
|
if (blurRadius > __FLT_EPSILON__) {
|
|
|
|
// Do this on the background thread to avoid blocking interaction
|
|
|
|
image = RCTBlurredImageWithRadius(image, blurRadius);
|
|
|
|
}
|
2015-10-20 12:00:50 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2015-12-08 11:29:08 +00:00
|
|
|
if (![source isEqual:strongSelf.source]) {
|
|
|
|
// Bail out if source has changed since we started loading
|
|
|
|
return;
|
|
|
|
}
|
2015-10-20 12:00:50 +00:00
|
|
|
if (image.reactKeyframeAnimation) {
|
2015-12-08 11:29:08 +00:00
|
|
|
[strongSelf.layer addAnimation:image.reactKeyframeAnimation forKey:@"contents"];
|
2015-10-20 12:00:50 +00:00
|
|
|
} else {
|
2015-12-08 11:29:08 +00:00
|
|
|
[strongSelf.layer removeAnimationForKey:@"contents"];
|
|
|
|
strongSelf.image = image;
|
|
|
|
}
|
|
|
|
if (error) {
|
|
|
|
if (strongSelf->_onError) {
|
|
|
|
strongSelf->_onError(@{ @"error": error.localizedDescription });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (strongSelf->_onLoad) {
|
|
|
|
strongSelf->_onLoad(nil);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (strongSelf->_onLoadEnd) {
|
|
|
|
strongSelf->_onLoadEnd(nil);
|
2015-07-15 20:17:13 +00:00
|
|
|
}
|
2015-10-20 12:00:50 +00:00
|
|
|
});
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reactSetFrame:(CGRect)frame
|
|
|
|
{
|
|
|
|
[super reactSetFrame:frame];
|
2015-10-19 17:24:01 +00:00
|
|
|
|
|
|
|
if (!self.image || self.image == _defaultImage) {
|
2015-09-07 00:06:35 +00:00
|
|
|
_targetSize = frame.size;
|
2015-07-14 11:06:17 +00:00
|
|
|
[self reloadImage];
|
2015-12-08 11:29:08 +00:00
|
|
|
} else if ([self sourceNeedsReload]) {
|
2015-09-23 14:30:37 +00:00
|
|
|
CGSize imageSize = self.image.size;
|
2016-01-20 19:03:22 +00:00
|
|
|
CGSize idealSize = RCTTargetSize(imageSize, self.image.scale, frame.size,
|
|
|
|
RCTScreenScale(), (RCTResizeMode)self.contentMode, YES);
|
2015-09-23 14:30:37 +00:00
|
|
|
|
|
|
|
if (RCTShouldReloadImageForSizeChange(imageSize, idealSize)) {
|
|
|
|
if (RCTShouldReloadImageForSizeChange(_targetSize, idealSize)) {
|
2015-12-08 11:29:08 +00:00
|
|
|
RCTLogInfo(@"[PERF IMAGEVIEW] Reloading image %@ as size %@", _source.imageURL, NSStringFromCGSize(idealSize));
|
2015-09-25 09:26:25 +00:00
|
|
|
|
2016-01-20 19:03:22 +00:00
|
|
|
// 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.
|
2015-09-23 14:30:37 +00:00
|
|
|
_targetSize = idealSize;
|
|
|
|
[self reloadImage];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Our existing image is good enough.
|
|
|
|
[self cancelImageLoad];
|
|
|
|
_targetSize = imageSize;
|
2015-07-14 11:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-08-04 15:17:39 +00:00
|
|
|
if (!self.window) {
|
2016-01-20 19:03:22 +00:00
|
|
|
// Don't keep self alive through the asynchronous dispatch, if the intention
|
|
|
|
// was to remove the view so it would deallocate.
|
2015-09-30 16:37:54 +00:00
|
|
|
__weak typeof(self) weakSelf = self;
|
|
|
|
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
__strong typeof(self) strongSelf = weakSelf;
|
|
|
|
if (!strongSelf) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-20 19:03:22 +00:00
|
|
|
// If we haven't been re-added to a window by this run loop iteration,
|
|
|
|
// clear out the image to save memory.
|
2015-09-30 16:37:54 +00:00
|
|
|
if (!strongSelf.window) {
|
|
|
|
[strongSelf clearImage];
|
|
|
|
}
|
|
|
|
});
|
2015-10-19 17:24:01 +00:00
|
|
|
} else if (!self.image || self.image == _defaultImage) {
|
2015-07-21 16:10:52 +00:00
|
|
|
[self reloadImage];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
@end
|