I've a ploblem with image Source and Obj's parameter.
Inside the local object I have IMG_NAME: 'A.png' like so:
I would like concatenate this value with source like
but I know require doesn't support concatenate so I decided ask you this question about the best way to solve this problem.
How can I try to solve this problem (my solve screenshot), and error image (Error reponse)
If you can modify the local object change to this
{
"id_letter": "1",
"name_letter": "A",
"image" require("./app/assets/images/A.png")
}
and then Component change to
<Image style={styles.letter_image} source={data.image} />
Edit Another Method use switch case statement
require not support dynamic path. Use switch case statement to solve this
function getImage(img_name) {
switch(img_name) {
case "A.png": return require("./app/assets/images/A.png");
case "B.png": return require("./app/assets/images/B.png");
}
}
<Image style={styles.letter_image} source={getImage(data.img_name)} />
I think there are 4 simple way to use a dynamic source in the Image Component:
1) You can use images that are already bundled into the app:
Images From Hybrid App's Resources If you are building a hybrid
app (some UIs in React Native, some UIs in platform code) you can
still use images that are already bundled into the app.
For images included via Xcode asset catalogs or in the Android
drawable folder, use the image name without the extension:
< Image source={{uri: 'app_icon'}} style={{width: 40, height: 40}} />
For images in the Android assets folder, use the asset:/ scheme:
< Image source={{uri: 'asset:/app_icon.png'}} style={{width: 40,
height: 40}} />
These approaches provide no safety checks. It's up to you to guarantee
that those images are available in the application. Also you have to
specify image dimensions manually.
https ://facebook.github.io/react-native/docs/images.html#images-from-hybrid-app-s-resources
2) You can use require('imagePath') into the object.
{
"id_letter": "1",
"name_letter":"A",
"img_name":require('./app/assets/images/imgname.jpg');
}
3) You can storage the images in the web and you can use:
<Image source={{uri: 'http: //site.com/app/assets/images/'+data.img_name}}/>
4) You can storage the images using the library https://github.com/wkh237/react-native-fetch-blob.
Related
What can I do to further improve image load speeds?
Am I using the current tools correctly?
I'm learning Next.js and for loading images, my current project uses Next/Image and has sharp.js installed. My load times within reason.
Here is how I'm using the Image tag, feedback of any kind is appreciated.
// items is an array of image urls
{items.map((src, index) => {
return (
<Image
src={src}
alt=""
layout="fill"
objectFit="cover"
priority={index === 0}
></Image>
);
})}
I have an app that can be styled for different customers. I use the styles.xml to style the app for each customer. However, I need to compile the app, every time I need to change the styles. I want to avoid this by getting these style definition from an outside file configuration, and then apply them at run time. I´m already doing this for other resources like images and texts/labels.
Is this possible? How?
Regards
I'll give an example for a color.
If a style has <Setter Property="BackgroundColor" Value="{StaticResource Color1}" />, then that refers to a Resource with key Color1.
In App.xaml.cs, read the config, use it to construct a Color instance. then set that Resource:
Color color1 = ...;
Application.Current.Resources["Color1"] = color1;
My issue is for example for resources like "android:colorPressedHighlight" that are specified in the styles.xml in the Android project.
The ColorPressedHighlight field is const. It is Compile time constant and could not assign value at runtime.
MS official docs: https://learn.microsoft.com/en-us/dotnet/api/android.resource.attribute.colorpressedhighlight?view=xamarin-android-sdk-12
I have a problem with serving a static image from my public/images folder.
It correctly displays in the files I've been working on so far EXCEPT the dynamic file [itemId].js (shown in the picture below)
The most interesting clue I have is this error in the console.
http://localhost:3000/items/_next/static/image/public/images/banner-big.f496e182c5dd297c1d1c030d9b7436d8.png?auto=format&fit=max&w=1920 404 (Not Found)
It tries to serve from /items, which is not what I want at all...
Does anyone know what's the trick behind this? If you need more informations, let me know.
// in components/Header.js (which is wrapping all of my pages)
import bannerBig from "../public/images/banner-big.png";
// then i use it this way with the Image component from next/image
<Image src={bannerBig} alt="midnight city" width={1920} height={800}/>
The img generated by the <Image /> component looks like this and is the same accross all of my pages:
<img alt="midnight city" src="_next/static/image/public/images/banner-big.f496e182c5dd297c1d1c030d9b7436d8.png?auto=format&fit=max&w=3840" decoding="async" style="position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%" srcset="_next/static/image/public/images/banner-big.f496e182c5dd297c1d1c030d9b7436d8.png?auto=format&fit=max&w=1920 1x, _next/static/image/public/images/banner-big.f496e182c5dd297c1d1c030d9b7436d8.png?auto=format&fit=max&w=3840 2x">
so I finally fixed the problem by adding a '/' before my static image path so it's always being served from the root
<Image
src={"/" + bannerBig.src}
alt="midnight city"
width={bannerBig.width}
height={bannerBig.height}
/>
My React Native 0.63.2 app will show images stored in cloud storage with code which is similar to below
<View style={styles.container}>
<Image
style={styles.stretch}
source={{uri:'https://filefolder.download-only.com/image1.jpg'}}
/>
</View>
The problem is that this https://filefoler.download-only.com/image1.jpg only allows to download file to local disk before opening it. One option I can think of is to download and save the image in a temp file locally and display it with source={{uri:file:///path_to_image_file}}. But this option would require explicit downloading of each image even before user clicks and opens it. Is it possible to make the download-only image behaving like a click-able image url? Ex, add "data:image/jpeg;base64," before download-only url (something like source= {{uri:"data:image/png;base64,"+"https://filefolder.download-only.com/image1.jpg")
Use rn-fetch-blob for this: https://github.com/joltup/rn-fetch-blob
Fetch image as a blob
Convert blob to base64 to display image, use following code to convert blob into base64
var reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
var base64data = reader.result;
console.log(base64data);
}
After ran some test code with a URL copied from the cloud storage, it turns out that the image can be displayed by <Image> as usual with the URL. There is no need to prefix the image path as we did for stream data.
<View style={styles.container}>
<Image
style={styles.stretch}
source={{uri:'https://filefolder.download-only.com/image1.jpg?expires=xxxxx&signature=xxxxxxxxxxxx'}}
/>
</View>
Please be noted that in browser the url (contains the authorization signature) only allows download the image to local file system. <Image> tag however will download the image first and display it in render view.
In my XAML code I have written this:
<Image Source="/Images/male32.png" Visibility="Visible" />
and my project file structure looks like so:
But my image is just not loading?? As you can see I'm trying to add the male32.png image that I have in the Images folder in my project..
Check the build action of the Image.
Example:
When you use "Build Action" of type "Content" then you access your image in this way:
<Image Stretch="None" Source="/images/appbar.cancel.rest.png"/>
When you use "Build Action" of type "Resource" then you access your image in this way:
<Image Source="/WP7SampleProject3;component/images/appbar.feature.email.rest.png"/>
Okay I figured it out, turns out it didn't have anything to do with the BuildAction property, all I had to do in the image path was add ".." e.g. "../Images/male32.png".
Stupid issue that's not even supposed to be an issue.
Nonetheless I have read that BuildAction could also be a potential reason ones image is not displaying. So worth checking out if you have problems with images not displaying.