I can't get resizeMode='contain' to work for images loaded remotely, wondering if there's something I've done wrong.
Render Method:
return (
<View style={styles.internal_button_avatar_container}>
<Image
style={styles.internal_button_avatar_image}
source={{ uri: this.state.imageURL }}
loadingIndicatorSource={require('../assets/icons/PageLink-Loading.png')}
resizeMode={'contain'}
/>
</View>
);
Styles (relevant section):
internal_button_avatar_container: {
marginRight: 10,
width: 40,
height: 40,
},
internal_button_avatar_image: {
width: 40,
height: 40,
},
I've tried resizeMode='contain' and resizeMode='center' but the image always is cropped around the outside of it's frame. Screenshot showing cropped icon.. I've also tried with undefined height and width on the image.
The cropping only appears to happen if the source image is not the same aspect ratio as the frame, a square source will result in perfectly scaled image.
I tried to reproduce your situation, but I haven't problems with that in this snack:
https://snack.expo.io/#gersonmontenegro/resizemode
The original size of the image is bigger than the container, even it has a different aspect radio, and it has loaded according to the internal_button_avatar_image.
Is ok for you?
Turns out I was getting the image from an endpoint in an API that auto-cropped the original image - should've checked the source image before everything else!
Related
I am trying to decrease the height of Image component in Next.js. But, When I increase the width, the height has no affect. The image height stays the same as width. If width is 100, the image becomes 100x100 but, I need 100x50. How can I achieve that?
Below is my code
<Image
alt="Mountains"
width={80}
height={50}
src={Dog}
style={{ objectFit: 'contain' }}
/>
I want to set the width of an Image component to be 100% of my container's width and the height to be set according to the result width value and image's aspect ratio.
My container has some padding, which means that Dimensions.get('window').width is not useful to me since it does not gives me the value that results from image's width being set to 100%. Other scenarios also can't afford the Dimensions API as a acceptable solution.
Here's an code example:
<View
style={{
flex: 1,
paddingHorizontal: 20,
}}>
<Image
style={{
flex: 1, // Not what I want => just so I can see the image
width: '100%',
// height: 'auto', // The behavior that I want
backgroundColor: 'red',
}}
resizeMode={'contain'}
source={{
uri: 'https://via.placeholder.com/200x400/000000',
}}
/>
</View>
Result:
The red background serves just to visualize that the Image component.
Using onLayout prop I can get the runtime value of the resulting width, and with the aid of the Image API i could calculate the desired height. The problem with that is that React Native will first render a 0 height component and just after that onLayout seems to be resolved, and that makes sense.
So?
Is there a way to get before hand the width value of a image component such as this (declared using relative width)?
Is automatically setting image height even possible in React Native? If so, how?
Notes:
- In the image docs of React Native there is an explanation of why they chose not to size image components automatically by default.
- At least in this case, Parent onLayout is resolved after its children.
In your componentDidMount you could calculate the image metrics, and then use that height and width to calculate the aspectRatio.
componentDidMount = () => {
Image.getSize(YourUri, (width, height) => {
this.setState({width, height});
});
}
In your image you will add the styles like this:
<Image
style={{
width: '100%',
backgroundColor: 'red',
aspectRatio: this.state.height !== null ? this.state.width / this.state.height : 1
}}
resizeMode={'contain'}
source={{
uri: 'https://via.placeholder.com/200x400/000000',
}}
/>
As stated in React native docs:
Aspect ratio controls the size of the undefined dimension of a node.
Aspect ratio is a non-standard property only available in React Native
and not CSS.
-On a node with a set width/height aspect ratio controls the size of the unset dimension
-On a node with a set flex basis aspect ratio controls the size of the node in the cross axis if unset
-On a node with a measure function aspect ratio works as though the measure function measures the flex basis
-On a node with flex grow/shrink aspect ratio controls the size of the node in the cross axis if unset
Aspect ratio takes min/max dimensions into account
I already extracted data from firebase and displaying them. Now i need to display images also. In firebase database i have the same structure as the image below
And I have a folder under my project that contains all images with the exact same name like for an example "Carl.png"
I extracted the photo content in and combined with the rest of the path : "Myproject/img/"+photo+".png" and it's displayed correctly in the screen. But when i try to affect it to image i get errors. And here is the code i tried
<Image style={{width: 50, height: 50}} source={{uri : (this.props.item.imageName)}}> </Image>
and i get this and if i try to put in <Image style={{width: 50, height: 50}} source={{ uri: <Text> this.props.item.imageName </Text> }}> </Image> i get this
and if i put this code source={require(this.props.item.imageName)}
here is what i get: unknown named module
Try loading it using require because you are loading it from a local storage, so your source attribute becomes:
source={ require('./img/'+photo+'.png') }
try with
<Image style={{width: 50, height: 50}} source={{uri : (this.props.item.imageName)}} />
You've got space between opening & closing tag.
actually, React native does not support the dynamic images even if they are stored locally and you just need to make their name dynamically variable. So, the only solution that i was obliged to use was to use a massive switch case and here the code i used:
switch (item.name) {
case 'carl':
return (
<Image source{require('pathUnderProject/carl.png')}/>
);
...
default:
return (
<View >
<Text>{'Null'}</Text>
</View>
);
I know it's not the best solution, but at least i'm sure it's working fine and it won't break in the production environment
Hello World,
I trying to crop an image like explain on the React-native Doc
<Image source={{uri: this.props.image, crop: {left: 50, top: 50, width: 100, height: 100}}} style={{height: 100, width: 100}}/>
But it's doesn't work the image is not cropped.
Any idea?
From the docs:
On the infrastructure side, the reason is that it allows us to attach metadata to this object. For example if you are using require('./my-icon.png'), then we add information about its actual location and size (don't rely on this fact, it might change in the future!). This is also future proofing, for example we may want to support sprites at some point, instead of outputting {uri: ...}, we can output {uri: ..., crop: {left: 10, top: 50, width: 20, height: 40}} and transparently support spriting on all the existing call sites.
React Native Image is not currently supporting image cropping, at least not the way you pointed, however you still have other options that will do the same job.
ImageEditor:
React Native Component, again from the docs:
Crop the image specified by the URI param. If URI points to a remote
image, it will be downloaded automatically. If the image cannot be
loaded/downloaded, the failure callback will be called.
Cropping doesn't require linking.
Image Crop Picker another package that offers cropping, but in a different way: Picking. Requires linking, but thankfully it also supports RN versions > 0.40.
I haven't used any of them, but if I were you, I would first try Image Editor, since you don't need any additional installation except importing.
you can use package for cropping the image. first package for uploading image and second for cropping the image.
react-native-image-picker
react-native-image-crop-picker
for uploading image you can use first package function like this
const pickImage = () => {
launchImageLibrary({quality: 0.5}, response => {
if (!response.didCancel) {
setUri(response.assets[0]);
setCropImg('');
}
});
};
for cropping the image you can use second package function like this
const Crop_img = () => {
ImagePicker.openCropper({
path: uri.uri,
width: dimensions.width - 30,
height: dimensions.height / 3.5,
maxFiles: 1,
showCropFrame: false,
}).then(image => {
console.log(image.path);
setCropImg(image.path);
});
};
I am trying to make an circular image view in Alloy appcelerator formerly Titanium like this
XML
<ImageView id="profile_photo" />
TSS
"#profile_photo_view_holder":{
width: 80,
height: 80,
borderRadius: 40,
borderWidth:2,
borderColor:"black"
}
The image view is rendering properly but jaggy in its edges looks like an anti aliasing problem.
I use image factory module to scale down also but no luck.
resizedImage = ImageFactory.imageAsResized(blob, {
width : 80,
height : 80,
quality : 0.9
});
$.profile_photo.image = resizedImage;
on Android you will need to make use of a module to achieve it.
This module is quite new and looks great:
https://github.com/m1ga/com.miga.roundview
I'm using this one in my project:
https://github.com/snowciety/sc.roundedavatar
Hope it helps