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-08-12 14:07:24 +00:00
|
|
|
#import "RCTImageUtils.h"
|
2015-07-14 11:06:17 +00:00
|
|
|
#import "RCTUtils.h"
|
|
|
|
|
|
|
|
#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`.
|
|
|
|
*/
|
|
|
|
static BOOL RCTShouldReloadImageForSizeChange(CGSize currentSize, CGSize idealSize) {
|
|
|
|
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
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setCapInsets:(UIEdgeInsets)capInsets
|
|
|
|
{
|
|
|
|
if (!UIEdgeInsetsEqualToEdgeInsets(_capInsets, capInsets)) {
|
|
|
|
_capInsets = capInsets;
|
2015-08-12 14:07:24 +00:00
|
|
|
[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-07-14 11:06:17 +00:00
|
|
|
- (void)setSrc:(NSString *)src
|
|
|
|
{
|
|
|
|
if (![src isEqual:_src]) {
|
|
|
|
_src = [src copy];
|
|
|
|
[self reloadImage];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-02 15:25:10 +00:00
|
|
|
+ (BOOL)srcNeedsReload:(NSString *)src
|
|
|
|
{
|
|
|
|
return
|
|
|
|
[src hasPrefix:@"http://"] ||
|
|
|
|
[src hasPrefix:@"https://"] ||
|
|
|
|
[src hasPrefix:@"assets-library://"] ||
|
|
|
|
[src hasPrefix:@"ph://"];
|
|
|
|
}
|
|
|
|
|
2015-08-12 14:07:24 +00:00
|
|
|
- (void)setContentMode:(UIViewContentMode)contentMode
|
|
|
|
{
|
|
|
|
if (self.contentMode != contentMode) {
|
|
|
|
super.contentMode = contentMode;
|
2015-09-02 15:25:10 +00:00
|
|
|
if ([RCTImageView srcNeedsReload:_src]) {
|
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-09-23 14:30:37 +00:00
|
|
|
if (_src && 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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-09-23 14:30:36 +00:00
|
|
|
_reloadImageCancellationBlock = [_bridge.imageLoader loadImageWithTag:_src
|
|
|
|
size:self.bounds.size
|
|
|
|
scale:RCTScreenScale()
|
|
|
|
resizeMode:self.contentMode
|
|
|
|
progressBlock:progressHandler
|
|
|
|
completionBlock:^(NSError *error, UIImage *image) {
|
2015-09-04 11:35:44 +00:00
|
|
|
if (image.reactKeyframeAnimation) {
|
|
|
|
[self.layer addAnimation:image.reactKeyframeAnimation forKey:@"contents"];
|
2015-07-14 11:06:17 +00:00
|
|
|
} else {
|
|
|
|
[self.layer removeAnimationForKey:@"contents"];
|
|
|
|
self.image = image;
|
|
|
|
}
|
2015-07-15 20:17:13 +00:00
|
|
|
if (error) {
|
|
|
|
if (_onError) {
|
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
|
|
|
_onError(@{ @"error": error.localizedDescription });
|
2015-07-15 20:17:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (_onLoad) {
|
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
|
|
|
_onLoad(nil);
|
2015-07-15 20:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_onLoadEnd) {
|
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
|
|
|
_onLoadEnd(nil);
|
2015-07-15 20:17:13 +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];
|
|
|
|
if (self.image == nil) {
|
2015-09-07 00:06:35 +00:00
|
|
|
_targetSize = frame.size;
|
2015-07-14 11:06:17 +00:00
|
|
|
[self reloadImage];
|
2015-09-02 15:25:10 +00:00
|
|
|
} else if ([RCTImageView srcNeedsReload:_src]) {
|
2015-09-23 14:30:37 +00:00
|
|
|
CGSize imageSize = self.image.size;
|
|
|
|
CGSize idealSize = RCTTargetSize(imageSize, self.image.scale, frame.size, RCTScreenScale(), self.contentMode, YES);
|
|
|
|
|
|
|
|
if (RCTShouldReloadImageForSizeChange(imageSize, idealSize)) {
|
|
|
|
if (RCTShouldReloadImageForSizeChange(_targetSize, idealSize)) {
|
2015-09-25 09:26:25 +00:00
|
|
|
RCTLogInfo(@"[PERF IMAGEVIEW] Reloading image %@ as size %@", _src, NSStringFromCGSize(idealSize));
|
|
|
|
|
2015-09-23 14:30:37 +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.
|
|
|
|
_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) {
|
2015-09-23 14:30:36 +00:00
|
|
|
[self clearImage];
|
2015-08-04 15:17:39 +00:00
|
|
|
} else if (self.src) {
|
2015-07-21 16:10:52 +00:00
|
|
|
[self reloadImage];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
@end
|