Skip to content

[Image] Add examples to UIExplorer, fix some bugs #1987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions Examples/UIExplorer/ImageExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,41 @@ var {
StyleSheet,
Text,
View,
ActivityIndicatorIOS
} = React;

var ImageCapInsetsExample = require('./ImageCapInsetsExample');

var NetworkImage = React.createClass({
watchID: (null: ?number),

getInitialState: function() {
return {
error: false,
loading: true,
progress: 0
};
},
render: function() {
var loader = this.state.loading ?
<View style={styles.progress}>
<Text>{this.state.progress}%</Text>
<ActivityIndicatorIOS style={{marginLeft:5}}/>
</View> : null;
return this.state.error ?
<Text>{this.state.error}</Text> :
<Image
source={this.props.source}
style={styles.base}
onLoadError={(e) => this.setState({error: e.nativeEvent.error})}
onLoadProgress={(e) => this.setState({progress: Math.max(0, Math.round(100 * e.nativeEvent.written / e.nativeEvent.total))}) }
onLoadEnd={() => this.setState({loading: false, error: false})}
onLoadAbort={() => this.setState({error: 'Loading has aborted'})} >
{loader}
</Image>
}
});

exports.displayName = (undefined: ?string);
exports.framework = 'React';
exports.title = '<Image>';
Expand Down Expand Up @@ -59,6 +90,20 @@ exports.examples = [
);
},
},
{
title: 'Error Handler',
render: function() {
return <NetworkImage source={{uri: 'http://TYPO_ERROR_facebook.github.io/react/img/logo_og.png'}} />
},
},
{
title: 'Image Download Progress',
render: function() {
return (
<NetworkImage source={{uri: 'http://facebook.github.io/origami/public/images/blog-hero.jpg?r=1'}}/>
);
},
},
{
title: 'Border Color',
render: function() {
Expand Down Expand Up @@ -300,6 +345,12 @@ var styles = StyleSheet.create({
width: 38,
height: 38,
},
progress: {
flex: 1,
alignItems: 'center',
flexDirection: 'row',
width: 100
},
leftMargin: {
marginLeft: 10,
},
Expand Down
1 change: 0 additions & 1 deletion Libraries/Image/RCTDownloadTaskWrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ - (NSURLSessionDownloadTask *)downloadData:(NSURL *)url progressBlock:(RCTDataPr
task.rct_completionBlock = completionBlock;
task.rct_progressBlock = progressBlock;

[task resume];
return task;
}

Expand Down
2 changes: 1 addition & 1 deletion Libraries/Image/RCTImageDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ - (RCTImageDownloadCancellationBlock)_downloadDataForURL:(NSURL *)url progressBl
runBlocks(NO, data, error);
}

if (response) {
if (response && !error) {
RCTImageDownloader *strongSelf = weakSelf;
NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data userInfo:nil storagePolicy:NSURLCacheStorageAllowed];
[strongSelf->_cache storeCachedResponse:cachedResponse forRequest:request];
Expand Down
10 changes: 6 additions & 4 deletions Libraries/Image/RCTNetworkImageView.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ - (void)setImageURL:(NSURL *)imageURL resetToDefaultImageWhileLoading:(BOOL)rese
{
if (![_imageURL isEqual:imageURL] && _downloadToken) {
[_imageDownloader cancelDownload:_downloadToken];
NSDictionary *event = @{ @"target": self.reactTag };
[_eventDispatcher sendInputEventWithName:@"loadAbort" body:event];
_downloadToken = nil;
}

Expand Down Expand Up @@ -146,8 +148,8 @@ - (void)setImageURL:(NSURL *)imageURL resetToDefaultImageWhileLoading:(BOOL)rese
loadEndHandler();
});
} else if (error) {
errorHandler([error description]);
}
errorHandler([error localizedDescription]);
}
}];
} else {
_downloadToken = [_imageDownloader downloadImageForURL:imageURL
Expand All @@ -170,8 +172,8 @@ - (void)setImageURL:(NSURL *)imageURL resetToDefaultImageWhileLoading:(BOOL)rese
loadEndHandler();
});
} else if (error) {
errorHandler([error description]);
}
errorHandler([error localizedDescription]);
}
}];
}
}
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Image/RCTNetworkImageViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ - (NSDictionary *)customDirectEventTypes
return @{
@"loadStart": @{ @"registrationName": @"onLoadStart" },
@"loadProgress": @{ @"registrationName": @"onLoadProgress" },
@"loaded": @{ @"registrationName": @"onLoaded" },
@"loaded": @{ @"registrationName": @"onLoadEnd" },
@"loadError": @{ @"registrationName": @"onLoadError" },
@"loadAbort": @{ @"registrationName": @"onLoadAbort" },
};
Expand Down