2015-07-15 20:17:13 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTImageViewManager.h"
|
|
|
|
|
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
|
2016-11-23 15:47:52 +00:00
|
|
|
#import <React/RCTConvert.h>
|
|
|
|
|
Added getImageSize method
Summary:
public
This diff adds a `getSize()` method to `Image` to retrieve the width and height of an image prior to displaying it. This is useful when working with images from uncontrolled sources, and has been a much-requested feature.
In order to retrieve the image dimensions, the image may first need to be loaded or downloaded, after which it will be cached. This means that in principle you could use this method to preload images, however it is not optimized for that purpose, and may in future be implemented in a way that does not fully load/download the image data.
A fully supported way to preload images will be provided in a future diff.
The API (separate success and failure callbacks) is far from ideal, but until we agree on a unified standard, this was the most conventional way I could think of to implement it. If it returned a promise or something similar, it would be unique among all such APIS in the framework.
Please note that this has been a long time coming, in part due to much bikeshedding about what the API should look like, so while it's not unlikely that the API may change in future, I think having *some* way to do this is better than waiting until we can define the "perfect" way.
Reviewed By: vjeux
Differential Revision: D2797365
fb-gh-sync-id: 11eb1b8547773b1f8be0bc55ddf6dfedebf7fc0a
2016-01-01 02:50:26 +00:00
|
|
|
#import "RCTImageLoader.h"
|
2017-05-31 02:56:51 +00:00
|
|
|
#import "RCTImageShadowView.h"
|
2015-07-15 20:17:13 +00:00
|
|
|
#import "RCTImageView.h"
|
|
|
|
|
|
|
|
@implementation RCTImageViewManager
|
|
|
|
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2017-05-31 02:56:51 +00:00
|
|
|
- (RCTShadowView *)shadowView
|
|
|
|
{
|
|
|
|
return [RCTImageShadowView new];
|
|
|
|
}
|
|
|
|
|
2015-07-15 20:17:13 +00:00
|
|
|
- (UIView *)view
|
|
|
|
{
|
|
|
|
return [[RCTImageView alloc] initWithBridge:self.bridge];
|
|
|
|
}
|
|
|
|
|
2016-03-17 19:25:07 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(blurRadius, CGFloat)
|
2015-07-15 20:17:13 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(capInsets, UIEdgeInsets)
|
2015-12-08 11:29:08 +00:00
|
|
|
RCT_REMAP_VIEW_PROPERTY(defaultSource, defaultImage, UIImage)
|
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
|
|
|
RCT_EXPORT_VIEW_PROPERTY(onLoadStart, RCTDirectEventBlock)
|
|
|
|
RCT_EXPORT_VIEW_PROPERTY(onProgress, RCTDirectEventBlock)
|
|
|
|
RCT_EXPORT_VIEW_PROPERTY(onError, RCTDirectEventBlock)
|
2016-09-21 19:11:19 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(onPartialLoad, RCTDirectEventBlock)
|
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
|
|
|
RCT_EXPORT_VIEW_PROPERTY(onLoad, RCTDirectEventBlock)
|
|
|
|
RCT_EXPORT_VIEW_PROPERTY(onLoadEnd, RCTDirectEventBlock)
|
2016-06-22 11:13:22 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(resizeMode, RCTResizeMode)
|
2016-08-23 18:26:35 +00:00
|
|
|
RCT_REMAP_VIEW_PROPERTY(source, imageSources, NSArray<RCTImageSource *>);
|
2015-12-10 17:06:02 +00:00
|
|
|
RCT_CUSTOM_VIEW_PROPERTY(tintColor, UIColor, RCTImageView)
|
|
|
|
{
|
|
|
|
// Default tintColor isn't nil - it's inherited from the superView - but we
|
|
|
|
// want to treat a null json value for `tintColor` as meaning 'disable tint',
|
|
|
|
// so we toggle `renderingMode` here instead of in `-[RCTImageView setTintColor:]`
|
|
|
|
view.tintColor = [RCTConvert UIColor:json] ?: defaultView.tintColor;
|
|
|
|
view.renderingMode = json ? UIImageRenderingModeAlwaysTemplate : defaultView.renderingMode;
|
|
|
|
}
|
2015-07-15 20:17:13 +00:00
|
|
|
|
2016-06-07 12:29:13 +00:00
|
|
|
RCT_EXPORT_METHOD(getSize:(NSURLRequest *)request
|
Added getImageSize method
Summary:
public
This diff adds a `getSize()` method to `Image` to retrieve the width and height of an image prior to displaying it. This is useful when working with images from uncontrolled sources, and has been a much-requested feature.
In order to retrieve the image dimensions, the image may first need to be loaded or downloaded, after which it will be cached. This means that in principle you could use this method to preload images, however it is not optimized for that purpose, and may in future be implemented in a way that does not fully load/download the image data.
A fully supported way to preload images will be provided in a future diff.
The API (separate success and failure callbacks) is far from ideal, but until we agree on a unified standard, this was the most conventional way I could think of to implement it. If it returned a promise or something similar, it would be unique among all such APIS in the framework.
Please note that this has been a long time coming, in part due to much bikeshedding about what the API should look like, so while it's not unlikely that the API may change in future, I think having *some* way to do this is better than waiting until we can define the "perfect" way.
Reviewed By: vjeux
Differential Revision: D2797365
fb-gh-sync-id: 11eb1b8547773b1f8be0bc55ddf6dfedebf7fc0a
2016-01-01 02:50:26 +00:00
|
|
|
successBlock:(RCTResponseSenderBlock)successBlock
|
|
|
|
errorBlock:(RCTResponseErrorBlock)errorBlock)
|
|
|
|
{
|
2016-06-07 12:29:13 +00:00
|
|
|
[self.bridge.imageLoader getImageSizeForURLRequest:request
|
2016-08-19 17:31:42 +00:00
|
|
|
block:^(NSError *error, CGSize size) {
|
|
|
|
if (error) {
|
|
|
|
errorBlock(error);
|
|
|
|
} else {
|
|
|
|
successBlock(@[@(size.width), @(size.height)]);
|
|
|
|
}
|
|
|
|
}];
|
Added getImageSize method
Summary:
public
This diff adds a `getSize()` method to `Image` to retrieve the width and height of an image prior to displaying it. This is useful when working with images from uncontrolled sources, and has been a much-requested feature.
In order to retrieve the image dimensions, the image may first need to be loaded or downloaded, after which it will be cached. This means that in principle you could use this method to preload images, however it is not optimized for that purpose, and may in future be implemented in a way that does not fully load/download the image data.
A fully supported way to preload images will be provided in a future diff.
The API (separate success and failure callbacks) is far from ideal, but until we agree on a unified standard, this was the most conventional way I could think of to implement it. If it returned a promise or something similar, it would be unique among all such APIS in the framework.
Please note that this has been a long time coming, in part due to much bikeshedding about what the API should look like, so while it's not unlikely that the API may change in future, I think having *some* way to do this is better than waiting until we can define the "perfect" way.
Reviewed By: vjeux
Differential Revision: D2797365
fb-gh-sync-id: 11eb1b8547773b1f8be0bc55ddf6dfedebf7fc0a
2016-01-01 02:50:26 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 12:29:13 +00:00
|
|
|
RCT_EXPORT_METHOD(prefetchImage:(NSURLRequest *)request
|
|
|
|
resolve:(RCTPromiseResolveBlock)resolve
|
|
|
|
reject:(RCTPromiseRejectBlock)reject)
|
|
|
|
{
|
|
|
|
if (!request) {
|
|
|
|
reject(@"E_INVALID_URI", @"Cannot prefetch an image for an empty URI", nil);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[self.bridge.imageLoader loadImageWithURLRequest:request
|
|
|
|
callback:^(NSError *error, UIImage *image) {
|
|
|
|
if (error) {
|
|
|
|
reject(@"E_PREFETCH_FAILURE", nil, error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve(@YES);
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2015-07-15 20:17:13 +00:00
|
|
|
@end
|