mirror of
https://github.com/status-im/react-native.git
synced 2025-01-28 02:04:55 +00:00
8e70c7f003
Summary: This PR adds 4 native events to NetworkImage. ![demo](http://zippy.gfycat.com/MelodicLawfulCaecilian.gif) Using these events I could wrap `Image` component into something like: ```javascript class NetworkImage extends React.Component { getInitialState() { return { downloading: false, progress: 0 } } render() { var loader = this.state.downloading ? <View style={this.props.loaderStyles}> <ActivityIndicatorIOS animating={true} size={'large'} /> <Text style={{color: '#bbb'}}>{this.state.progress}%</Text> </View> : null; return <Image source={this.props.source} onLoadStart={() => this.setState({downloading: true}) } onLoaded={() => this.setState({downloading: false}) } onLoadProgress={(e)=> this.setState({progress: Math.round(100 * e.nativeEvent.written / e.nativeEvent.total)}); onLoadError={(e)=> { alert('the image cannot be downloaded because: ', JSON.stringify(e)); this.setState({downloading: false}); }}> {loader} </Image> } } ``` Useful on slow connections and server errors. There are dozen lines of Objective C, which I don't have experience with. There are neither specific tests nor documentation yet. And I do realize that you're already working right now on better `<Image/>` (pipeline, new asset management, etc.). So this is basically a proof concept of events for images, and if this idea is not completely wrong I could improve it or help somehow. Closes https://github.com/facebook/react-native/pull/1318 Github Author: Dmitriy Loktev <unknownliveid@hotmail.com>
52 lines
1.8 KiB
Objective-C
52 lines
1.8 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 <UIKit/UIKit.h>
|
|
|
|
#import "RCTDownloadTaskWrapper.h"
|
|
|
|
typedef void (^RCTDataDownloadBlock)(NSData *data, NSError *error);
|
|
typedef void (^RCTImageDownloadBlock)(UIImage *image, NSError *error);
|
|
|
|
@interface RCTImageDownloader : NSObject
|
|
|
|
+ (RCTImageDownloader *)sharedInstance;
|
|
|
|
/**
|
|
* Downloads a block of raw data and returns it. Note that the callback block
|
|
* will not be executed on the same thread you called the method from, nor on
|
|
* the main thread. Returns a token that can be used to cancel the download.
|
|
*/
|
|
- (id)downloadDataForURL:(NSURL *)url
|
|
progressBlock:(RCTDataProgressBlock)progressBlock
|
|
block:(RCTDataDownloadBlock)block;
|
|
|
|
/**
|
|
* Downloads an image and decompresses it a the size specified. The compressed
|
|
* image will be cached in memory and to disk. Note that the callback block
|
|
* will not be executed on the same thread you called the method from, nor on
|
|
* the main thread. Returns a token that can be used to cancel the download.
|
|
*/
|
|
- (id)downloadImageForURL:(NSURL *)url
|
|
size:(CGSize)size
|
|
scale:(CGFloat)scale
|
|
resizeMode:(UIViewContentMode)resizeMode
|
|
backgroundColor:(UIColor *)backgroundColor
|
|
progressBlock:(RCTDataProgressBlock)progressBlock
|
|
block:(RCTImageDownloadBlock)block;
|
|
|
|
/**
|
|
* Cancel an in-flight download. If multiple requets have been made for the
|
|
* same image, only the request that relates to the token passed will be
|
|
* cancelled.
|
|
*/
|
|
- (void)cancelDownload:(id)downloadToken;
|
|
|
|
@end
|