Native implementation of <Image> intrinsic content size on iOS

Summary:
Now intrinsic content size of <Image> is implemented natively on iOS and now it is actually
`intrinsicContentSize`, not just overrided `height` and `width` styles (which was incorrect and hacky).
This change also removes support of nested content inside <Image>.
This is a first commit in the row where we improve <Image> implementation.

Reviewed By: mmmulani

Differential Revision: D5189017

fbshipit-source-id: eab3defa3d86a5d4219b4f4925ab7460b58d760f
This commit is contained in:
Valentin Shergin 2017-10-23 19:37:30 -07:00 коммит произвёл Facebook Github Bot
Родитель c47759a9ae
Коммит 7bd0855650
8 изменённых файлов: 33 добавлений и 4 удалений

Просмотреть файл

@ -33,6 +33,7 @@ class ImageSnapshotTest extends React.Component<{}> {
render() {
return (
<Image
style={{position: 'absolute'}}
source={require('./blue_square.png')}
defaultSource={require('./red_square.png')}
onLoad={() => TestModule.verifySnapshot(this.done)} />

Просмотреть файл

@ -352,13 +352,11 @@ const Image = createReactClass({
const source = resolveAssetSource(this.props.source) || { uri: undefined, width: undefined, height: undefined };
let sources;
let style;
let style = flattenStyle([styles.base, this.props.style]) || {};
if (Array.isArray(source)) {
style = flattenStyle([styles.base, this.props.style]) || {};
sources = source;
} else {
const {width, height, uri} = source;
style = flattenStyle([{width, height}, styles.base, this.props.style]) || {};
const {uri} = source;
sources = [source];
if (uri === '') {

Просмотреть файл

@ -13,6 +13,7 @@
#import <React/RCTConvert.h>
#import <React/RCTEventDispatcher.h>
#import <React/RCTImageSource.h>
#import <React/RCTUIManager.h>
#import <React/RCTUtils.h>
#import <React/UIView+React.h>
@ -107,6 +108,22 @@ static NSDictionary *onLoadParamsForSource(RCTImageSource *source)
RCT_NOT_IMPLEMENTED(- (instancetype)init)
- (CGSize)intrinsicContentSize
{
// The first `imageSource` defines intrinsic content size.
RCTImageSource *imageSource = _imageSources.firstObject;
if (!imageSource) {
return CGSizeZero;
}
return imageSource.size;
}
- (void)updateIntrinsicContentSize
{
[_bridge.uiManager setIntrinsicContentSize:self.intrinsicContentSize forView:self];
}
- (void)updateWithImage:(UIImage *)image
{
if (!image) {
@ -176,6 +193,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
{
if (![imageSources isEqual:_imageSources]) {
_imageSources = [imageSources copy];
[self updateIntrinsicContentSize];
_needsReload = YES;
}
}

Двоичный файл не отображается.

До

Ширина:  |  Высота:  |  Размер: 166 KiB

После

Ширина:  |  Высота:  |  Размер: 164 KiB

Двоичный файл не отображается.

До

Ширина:  |  Высота:  |  Размер: 165 KiB

После

Ширина:  |  Высота:  |  Размер: 163 KiB

Двоичный файл не отображается.

До

Ширина:  |  Высота:  |  Размер: 166 KiB

После

Ширина:  |  Высота:  |  Размер: 164 KiB

Просмотреть файл

@ -634,6 +634,18 @@ exports.examples = [
return <ImageSizeExample source={fullImage} />;
},
},
{
title: 'Intrinsic Content Size',
description: 'Images have intrinsic content size which respects ' +
'to the first `source` image size.',
render: function() {
return (
<View>
<Image source={require('./uie_thumb_big.png')} style={{alignSelf: 'center'}} />
</View>
);
},
},
{
title: 'MultipleSourcesExample',
description:

Просмотреть файл

До

Ширина:  |  Высота:  |  Размер: 6.6 KiB

После

Ширина:  |  Высота:  |  Размер: 6.6 KiB