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-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-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-07-14 11:06:17 +00:00
|
|
|
- (void)reloadImage
|
|
|
|
{
|
|
|
|
if (_src && !CGSizeEqualToSize(self.frame.size, CGSizeZero)) {
|
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-07-27 15:48:31 +00:00
|
|
|
[_bridge.imageLoader loadImageWithTag:_src
|
|
|
|
size:self.bounds.size
|
|
|
|
scale:RCTScreenScale()
|
|
|
|
resizeMode:self.contentMode
|
|
|
|
progressBlock:progressHandler
|
2015-09-04 11:35:44 +00:00
|
|
|
completionBlock:^(NSError *error, UIImage *image) {
|
2015-07-15 20:17:13 +00:00
|
|
|
|
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 {
|
|
|
|
[self.layer removeAnimationForKey:@"contents"];
|
|
|
|
self.image = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (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-08-12 14:07:24 +00:00
|
|
|
CGSize idealSize = RCTTargetSize(self.image.size, self.image.scale, frame.size,
|
|
|
|
RCTScreenScale(), self.contentMode, YES);
|
2015-09-07 00:06:35 +00:00
|
|
|
CGFloat widthChangeFraction = ABS(_targetSize.width - idealSize.width) / _targetSize.width;
|
|
|
|
CGFloat heightChangeFraction = ABS(_targetSize.height - idealSize.height) / _targetSize.height;
|
2015-08-12 14:07:24 +00:00
|
|
|
|
2015-07-14 11:06:17 +00:00
|
|
|
// If the combined change is more than 20%, reload the asset in case there is a better size.
|
|
|
|
if (widthChangeFraction + heightChangeFraction > 0.2) {
|
2015-09-07 00:06:35 +00:00
|
|
|
_targetSize = idealSize;
|
2015-07-14 11:06:17 +00:00
|
|
|
[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
|
|
|
|
2015-08-04 15:17:39 +00:00
|
|
|
if (!self.window) {
|
2015-07-21 16:10:52 +00:00
|
|
|
[self.layer removeAnimationForKey:@"contents"];
|
|
|
|
self.image = nil;
|
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
|