问题
In react-native one can do:
const somePath = 'https://...'
<Image source={somePath} />
or
const somePath = 'https://...'
<Image source={{uri: somePath}} />
From what I understand about web addresses is that URIs are a superset of URLs and URNs.
Questions
- What are the potential problems associated with supplying a web address to
source
as a URL? What are the potential problems associated with supplying a web address to
source
as a URI?Which method of supplying an address of an image to
source
is more accurate, safer, and future proof?
回答1:
The first code example you provided won't work
const somePath = 'https://...'
<Image source={somePath} />
In general general you should use the source prop to provide a local image like so
const someLocalImage = require("./assets/someImageName.png");
<Image source={someLocalImage} />
and the uri to display a remote image like so
<Image source={{uri: "https://example.com/someRemoteImagePath.png" />
You can also use the uri to display base64 image data
<Image
source={{uri: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADMAAAAzCAYAAAA6oTAqAAAAEXRFWHRTb2Z0d2FyZQBwbmdjcnVzaEB1SfMAAABQSURBVGje7dSxCQBACARB+2/ab8BEeQNhFi6WSYzYLYudDQYGBgYGBgYGBgYGBgYGBgZmcvDqYGBgmhivGQYGBgYGBgYGBgYGBgYGBgbmQw+P/eMrC5UTVAAAAABJRU5ErkJggg=='}}
/>
Read more about it in the documentation
https://facebook.github.io/react-native/docs/image.html#source
来源:https://stackoverflow.com/questions/50249353/uri-vs-url-in-react-native