Why is this error in Image Component of React-Native? - image

I would like to background black opacity.
I've wrote this code.
<Image source={require('../assets/images/test_img1.png')} style={[s.lastReviewImage]}>
<View style={s.blackOverlay}/>
</Image>
lastReviewImage: {
flex:1,
width: null, height: null
},
blackOverlay: {
position: 'absolute',
top: 0,
right: 0,
bottom: 0,
left: 0,
backgroundColor: 'red',
opacity: 0.3
},
But, this is give me a error.
Error: The Image component cannot contain children. if you want to render content on top of the image, consider using absolute positioning.
I know what this error means but I've surely set absolute property in blackOverlay.
I've refer this guides.
Text Overlay Image with Darkened Opacity React Native
https://snack.expo.io/S15Lt3vjg
But, It's not working for me with the error.

If you want to set some background color or an image as the Screen Background, you need to use ImageBackground in place of Image.
<ImageBackground
source={yourSourceFile}
style={s.blackOverlay}>
<....yourContent...>
</ImageBackground>

Related

Image inside TouchableOpacity not showing

I'm currently trying to set up an image inside TouchableOpacity. It isn't showing and I'm stuck on why that is. Please let me know if you can help. TIA!
I've basically copied this: https://facebook.github.io/react-native/docs/touchableopacity from the official docs, but to no avail.
Thought maybe my TouchableOpacity element wasn't full screen but that's not the case (when I set the background color, I see it fill the screen).
<TouchableOpacity style={styles.button} onPress={this.onWaitingButtonPress.bind(this)}>
<Image style={styles.imagestyle} source={{uri: "https://www.dollargeneral.com/media/catalog/product/cache/image/e9c3970ab036de70892d86c6d221abfe/0/0/00870101.jpg"}}/>
</TouchableOpacity>
const styles = {
button: {
flex: 1,
alignItems: "stretch",
backgroundColor: "red"
},
imageStyle: {
flex: 1,
resizeMode: "contain"
}
};
I get the red background to fill the whole screen, but the image does not.
flex: 1 cover the entire width and height of its container. probably your view's dimensions are 0.
if your touchable is showing, try this;
imageStyle: {
width: "100%",
height: "100%"
}
if it's not;
button: {
width: 100,
height: 100
}
or,
button: {
width: "100%,
height: "100%"
}
so, you can understand the difference between them when trying these
Flex:1 in parent covers entire area but flex1 in image cover only height of entire screen. Here width becomes 0 since flex direction is column by default. Try setting some width lfor image ike "100%' 0r any other value.
Edited after the comment:
If it doesn't not work then add width to touchableopacity also

React Native: bottom align an image with source height taller than container

I'd like to be able to bottom align an image using Image or ImageBackground, but have been unsuccessful in doing so.
<View style={styles.moduleHeader}>
<ImageBackground
style={styles.image}
source={{ uri: image }}
resizeMode="cover">
<Text>text</Text>
</ImageBackground>
</View>
moduleHeader: {
height: 200,
},
image: {
width: Layout.viewport.width, //device width
height: 200,
position: "absolute",
bottom: 0,
},
Other attempts include negative top margin and padding.
I've also messed around with aspect ratio, but that needs to be more of a precise number, and I don't need that level of precision.
The inclusion of absolute and bottom have no effect that I can tell...
Basically Id like to mimic the effect background-position: center bottom; has in CSS.
I don't mind if the image is cropped horizontally, just that the bottom of the image is aligned with the bottom of the container.
I haven't been able to find with any certainty that this is even possible in React-Native, so confirmation of that theory would constitute a correct answer.
As always any and all direction is greatly appreciated so thanks in advance!
You need to remove position and bottom tag from ImageBackground style and add into style of View
Something like this
moduleHeader: {
height: 200,
width:'100%',
position: "absolute",
bottom: 0,
},
image: {
width:'100%', //device width
height: 200,
backgroundColor:"#000",
},
Please check this snack expo code
https://snack.expo.io/#vishal7008/again-bottom-ui

React Native cant set image as background

I've been trying to set an image as background on a react native app, without success.
Here is my Image tag.
<Image
source={require('../img/cat.jpg')}
style={styles.backgroundImage}
blurRadius={1} />
It is supossed to contain children element, that should render on top.
The style is as it follows:
backgroundImage: {
flex: 1,
width: null,
height: null,
resizeMode: 'cover',
},
At the moment, children element are rendered on bottom of screen, after the image, and not on top of it.
How im supossed to make this work?
RN now provides the <ImageBackground> component as described in the docs and used as
return (
<ImageBackground source={...}>
<Text>Inside</Text>
</ImageBackground>
);
Alternatively, you can use position: 'absolute', top: 0, bottom: 0, left: 0, right: 0, resizeMode: 'stretch'//or 'contain'
with your Image styling.

Nest inside <Image> but behind the image

The problem I'm having is that the image inside the image, is on top of the outside image, the outside image is a half transparent image so I want it to be ON TOP of the nested image, as an overlay image, what can I do?
<Image
style={it}
source={require('../../img/Rarities/red.png')}
>
<View>
{
item[2] != null ?
<View style={{ width: 15, height: 15, backgroundColor: item[2].Color }} />
: null
}
<Image
style={{height:'70%',width:'70%'}}
source={{ uri: item[1].base64 }}
/>
</View>
</Image>
I tried:
changing the zIndex of the images didn't work..
In React Native, components are rendered in the order they are defined - therefore it can be tricky to reverse the order and render a parent on top of a child.
Instead, you can render the images as siblings, and use a parent container component with a little position: absolute trickery to get the images to align on top of each other.
For the following view structure:
<View style={styles.imageContainer}>
<Image source={{uri: image1}} style={styles.bottom} />
<Image source={{uri: image2}} style={styles.top} />
</View>
You can achieve this effect with following styles. See inline comments for explanation:
const styles = StyleSheet.create({
// The container controls the top image size
imageContainer: {
width: 200,
height: 200
},
bottom: {
// horizontal margin: (100% -width) / 2
marginHorizontal: '15%',
// vertical margin: (100% - height) / 2
marginVertical: '15%',
width: '70%',
height: '70%'
},
top: {
// positioned absolutely
position: 'absolute',
opacity: 0.5,
// full width/height of imageContainer
width: '100%',
height: '100%'
},
});
You can see it in action in this Snack demo.
I'm not sure that i understand what are you meaning correctly but you can use the Image components like this if you want to have them inside each other:
<View>
<Image
style={{flex:1}}
source={require('./img/favicon.png')}
/>
<Image
style={{height:'30',width:'30', position:'absolute'}}
source={require('./img/favicon.png')}
/>
</View>
Note: the Image sources and the dimensions should be replaced.

How to crop image in a circle shape in Titanium?

You can crop an image by setting it to the background of a label and then setting the label to the size you want, but is there a way to crop it in a circle shape?
Thanks!
Setting borderRadius to half of the images width/height will give you a circle shaped image.
var imageView = Ti.UI.createImageView({
image : '/images/myImage.png',
width : 60,
height : 60,
borderRadius : 30
});
win.add(imageView);
For cropping images, imageAsCropped is also quite handy.
Very simple solution that use titanium ui. Without corners to be pixelated (Android).
<View id="userFaceContainer">
<ImageView id="avatar" />
<View id="userFaceInnerBorder" />
</View>
and styles
"#avatar": {
width: 90,
height: 90
}
"#userFaceContainer": {
width: 90,
height: 90,
center: {
x:"50%",
y:"50%"
},
borderRadius: 45,
borderColor: "#669dd3",
borderWidth:2
}
"#userFaceInnerBorder": {
width: 88,
height: 88,
borderRadius: 44,
borderColor: "#669dd3",
borderWidth:2,
opacity: 0.7
}

Resources