mirror of
https://github.com/zoriya/react-native-web.git
synced 2026-05-31 01:36:11 +00:00
[fix] Image resizeMode for 'center' and 'repeat'
React Native will scale down an image to fit its container before centering or repeating it.
This commit is contained in:
+31
-3
@@ -89,6 +89,7 @@ const createTintColorSVG = (tintColor, id) =>
|
|||||||
) : null;
|
) : null;
|
||||||
|
|
||||||
type State = {
|
type State = {
|
||||||
|
layout: Object,
|
||||||
shouldDisplaySource: boolean
|
shouldDisplaySource: boolean
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -144,7 +145,7 @@ class Image extends Component<*, State> {
|
|||||||
// If an image has been loaded before, render it immediately
|
// If an image has been loaded before, render it immediately
|
||||||
const uri = resolveAssetUri(props.source);
|
const uri = resolveAssetUri(props.source);
|
||||||
const shouldDisplaySource = ImageUriCache.has(uri);
|
const shouldDisplaySource = ImageUriCache.has(uri);
|
||||||
this.state = { shouldDisplaySource };
|
this.state = { layout: {}, shouldDisplaySource };
|
||||||
this._imageState = getImageState(uri, shouldDisplaySource);
|
this._imageState = getImageState(uri, shouldDisplaySource);
|
||||||
this._filterId = filterId;
|
this._filterId = filterId;
|
||||||
filterId++;
|
filterId++;
|
||||||
@@ -191,12 +192,12 @@ class Image extends Component<*, State> {
|
|||||||
blurRadius,
|
blurRadius,
|
||||||
defaultSource,
|
defaultSource,
|
||||||
draggable,
|
draggable,
|
||||||
onLayout,
|
|
||||||
source,
|
source,
|
||||||
testID,
|
testID,
|
||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
capInsets,
|
capInsets,
|
||||||
onError,
|
onError,
|
||||||
|
onLayout,
|
||||||
onLoad,
|
onLoad,
|
||||||
onLoadEnd,
|
onLoadEnd,
|
||||||
onLoadStart,
|
onLoadStart,
|
||||||
@@ -270,7 +271,7 @@ class Image extends Component<*, State> {
|
|||||||
{...other}
|
{...other}
|
||||||
accessibilityLabel={accessibilityLabel}
|
accessibilityLabel={accessibilityLabel}
|
||||||
accessible={accessible}
|
accessible={accessible}
|
||||||
onLayout={onLayout}
|
onLayout={this._createLayoutHandler(finalResizeMode)}
|
||||||
style={[
|
style={[
|
||||||
styles.root,
|
styles.root,
|
||||||
this.context.isInAParentText && styles.inline,
|
this.context.isInAParentText && styles.inline,
|
||||||
@@ -283,6 +284,7 @@ class Image extends Component<*, State> {
|
|||||||
style={[
|
style={[
|
||||||
styles.image,
|
styles.image,
|
||||||
resizeModeStyles[finalResizeMode],
|
resizeModeStyles[finalResizeMode],
|
||||||
|
this._getBackgroundSize(finalResizeMode),
|
||||||
backgroundImage && { backgroundImage },
|
backgroundImage && { backgroundImage },
|
||||||
filters.length > 0 && { filter: filters.join(' ') }
|
filters.length > 0 && { filter: filters.join(' ') }
|
||||||
]}
|
]}
|
||||||
@@ -308,6 +310,32 @@ class Image extends Component<*, State> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_createLayoutHandler = resizeMode => {
|
||||||
|
const { onLayout } = this.props;
|
||||||
|
if (resizeMode === 'center' || resizeMode === 'repeat' || onLayout) {
|
||||||
|
return e => {
|
||||||
|
const { layout } = e.nativeEvent;
|
||||||
|
onLayout && onLayout(e);
|
||||||
|
this.setState(() => ({ layout }));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
_getBackgroundSize = resizeMode => {
|
||||||
|
if (this._imageRef && (resizeMode === 'center' || resizeMode === 'repeat')) {
|
||||||
|
const { naturalHeight, naturalWidth } = this._imageRef;
|
||||||
|
const { height, width } = this.state.layout;
|
||||||
|
if (naturalHeight && naturalWidth && height && width) {
|
||||||
|
const scaleFactor = Math.min(1, width / naturalWidth, height / naturalHeight);
|
||||||
|
const x = Math.ceil(scaleFactor * naturalWidth);
|
||||||
|
const y = Math.ceil(scaleFactor * naturalHeight);
|
||||||
|
return {
|
||||||
|
backgroundSize: `${x}px ${y}px`
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
_onError = () => {
|
_onError = () => {
|
||||||
const { onError, source } = this.props;
|
const { onError, source } = this.props;
|
||||||
this._updateImageState(STATUS_ERRORED);
|
this._updateImageState(STATUS_ERRORED);
|
||||||
|
|||||||
Reference in New Issue
Block a user