mirror of
https://github.com/status-im/react-native.git
synced 2025-01-10 01:25:39 +00:00
848839858b
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).
199 lines
4.9 KiB
Objective-C
199 lines
4.9 KiB
Objective-C
/**
|
|
* 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.
|
|
*/
|
|
|
|
#import "RCTImageView.h"
|
|
|
|
#import "RCTBridge.h"
|
|
#import "RCTConvert.h"
|
|
#import "RCTEventDispatcher.h"
|
|
#import "RCTGIFImage.h"
|
|
#import "RCTImageLoader.h"
|
|
#import "RCTImageUtils.h"
|
|
#import "RCTUtils.h"
|
|
|
|
#import "UIView+React.h"
|
|
|
|
@interface RCTImageView ()
|
|
|
|
@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;
|
|
|
|
@end
|
|
|
|
@implementation RCTImageView
|
|
{
|
|
RCTBridge *_bridge;
|
|
}
|
|
|
|
- (instancetype)initWithBridge:(RCTBridge *)bridge
|
|
{
|
|
if ((self = [super init])) {
|
|
_bridge = bridge;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
|
|
|
- (void)updateImage
|
|
{
|
|
UIImage *image = self.image;
|
|
if (!image) {
|
|
return;
|
|
}
|
|
|
|
// Apply rendering mode
|
|
if (_renderingMode != image.renderingMode) {
|
|
image = [image imageWithRenderingMode:_renderingMode];
|
|
}
|
|
|
|
// 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];
|
|
}
|
|
|
|
// Apply trilinear filtering to smooth out mis-sized images
|
|
self.layer.minificationFilter = kCAFilterTrilinear;
|
|
self.layer.magnificationFilter = kCAFilterTrilinear;
|
|
|
|
super.image = image;
|
|
}
|
|
|
|
- (void)setImage:(UIImage *)image
|
|
{
|
|
image = image ?: _defaultImage;
|
|
if (image != super.image) {
|
|
super.image = image;
|
|
[self updateImage];
|
|
}
|
|
}
|
|
|
|
- (void)setCapInsets:(UIEdgeInsets)capInsets
|
|
{
|
|
if (!UIEdgeInsetsEqualToEdgeInsets(_capInsets, capInsets)) {
|
|
_capInsets = capInsets;
|
|
[self updateImage];
|
|
}
|
|
}
|
|
|
|
- (void)setRenderingMode:(UIImageRenderingMode)renderingMode
|
|
{
|
|
if (_renderingMode != renderingMode) {
|
|
_renderingMode = renderingMode;
|
|
[self updateImage];
|
|
}
|
|
}
|
|
|
|
- (void)setSrc:(NSString *)src
|
|
{
|
|
if (![src isEqual:_src]) {
|
|
_src = [src copy];
|
|
[self reloadImage];
|
|
}
|
|
}
|
|
|
|
- (void)setContentMode:(UIViewContentMode)contentMode
|
|
{
|
|
if (self.contentMode != contentMode) {
|
|
super.contentMode = contentMode;
|
|
if ([RCTImageLoader isAssetLibraryImage:_src] || [RCTImageLoader isRemoteImage:_src]) {
|
|
[self reloadImage];
|
|
}
|
|
}
|
|
}
|
|
|
|
- (void)reloadImage
|
|
{
|
|
if (_src && !CGSizeEqualToSize(self.frame.size, CGSizeZero)) {
|
|
|
|
if (_onLoadStart) {
|
|
_onLoadStart(nil);
|
|
}
|
|
|
|
RCTImageLoaderProgressBlock progressHandler = nil;
|
|
if (_onProgress) {
|
|
progressHandler = ^(int64_t loaded, int64_t total) {
|
|
_onProgress(@{
|
|
@"loaded": @((double)loaded),
|
|
@"total": @((double)total),
|
|
});
|
|
};
|
|
}
|
|
|
|
[_bridge.imageLoader loadImageWithTag:_src
|
|
size:self.bounds.size
|
|
scale:RCTScreenScale()
|
|
resizeMode:self.contentMode
|
|
progressBlock:progressHandler
|
|
completionBlock:^(NSError *error, id image) {
|
|
|
|
if ([image isKindOfClass:[CAAnimation class]]) {
|
|
[self.layer addAnimation:image forKey:@"contents"];
|
|
} else {
|
|
[self.layer removeAnimationForKey:@"contents"];
|
|
self.image = image;
|
|
}
|
|
if (error) {
|
|
if (_onError) {
|
|
_onError(@{ @"error": error.localizedDescription });
|
|
}
|
|
} else {
|
|
if (_onLoad) {
|
|
_onLoad(nil);
|
|
}
|
|
}
|
|
if (_onLoadEnd) {
|
|
_onLoadEnd(nil);
|
|
}
|
|
}];
|
|
} else {
|
|
[self.layer removeAnimationForKey:@"contents"];
|
|
self.image = nil;
|
|
}
|
|
}
|
|
|
|
- (void)reactSetFrame:(CGRect)frame
|
|
{
|
|
[super reactSetFrame:frame];
|
|
if (self.image == nil) {
|
|
[self reloadImage];
|
|
} else if ([RCTImageLoader isAssetLibraryImage:_src] || [RCTImageLoader isRemoteImage:_src]) {
|
|
|
|
// Get optimal image size
|
|
CGSize currentSize = self.image.size;
|
|
CGSize idealSize = RCTTargetSize(self.image.size, self.image.scale, frame.size,
|
|
RCTScreenScale(), self.contentMode, YES);
|
|
|
|
CGFloat widthChangeFraction = ABS(currentSize.width - idealSize.width) / currentSize.width;
|
|
CGFloat heightChangeFraction = ABS(currentSize.height - idealSize.height) / currentSize.height;
|
|
|
|
// If the combined change is more than 20%, reload the asset in case there is a better size.
|
|
if (widthChangeFraction + heightChangeFraction > 0.2) {
|
|
[self reloadImage];
|
|
}
|
|
}
|
|
}
|
|
|
|
- (void)didMoveToWindow
|
|
{
|
|
[super didMoveToWindow];
|
|
|
|
if (!self.window) {
|
|
[self.layer removeAnimationForKey:@"contents"];
|
|
self.image = nil;
|
|
} else if (self.src) {
|
|
[self reloadImage];
|
|
}
|
|
}
|
|
|
|
@end
|