react-native-htmlview icon indicating copy to clipboard operation
react-native-htmlview copied to clipboard

Is it possible to have <img>s with local URIs?

Open pekkanikolaus opened this issue 8 years ago • 5 comments

Does react-native-htmlview support <img> tags with images local to the project?

I can't seem to get it to work with any path, even when they work as expected when passing them to a RN <Image> element.

So this works:

<Image source={require('../Images/logo.png')}>

but this doesn't:

 const htmlContent = `<img src="../Images/logo.png">`;
 <HTMLView value={htmlContent} />

I'm getting

Failed to get size for image: ../Images/logo.png

pekkanikolaus avatar Jul 21 '17 21:07 pekkanikolaus

@pekkanikolaus at the moment it's assumed that the resource is remote: the { uri: ... } object is passed to the underlying Image component. That should be a fairly easy fix, so if you'd like to PR it feel free! Otherwise it'll join the 'nice to have' feature list :wink:

bas-ie avatar Jul 22 '17 02:07 bas-ie

@richchurcher Thanks! Still new to all this, but will give it a try!

pekkanikolaus avatar Jul 22 '17 08:07 pekkanikolaus

OK! So I gave this a try, but there's a couple of snags.

I added support for local URIs by checking whether a local attribute in the <img> tag is set. If it is, I will do a require(props.attribs.src), rather than using the src string itself as the image source. So far, so good.

One snag is that there's no built-in way to determine the dimensions of a local image bundled with the project - Image.getSize() will just balk - so one would either have to use a 3rd party module to do that, or explicitly specify the image's dimensions in the <img> tag. The latter is what I settled for for the moment.

The other, more grave problem is that for require(props.attribs.src) to work, the resource path must be declared statically in the code! Ugh!

There's one exception, but it's not very helpful: resources that are declared through XCode asset catalogs or in the Android drawable folder can apparently be require()d dynamically.

Having to manually require() every bundled resource you're going to mention in the HTML, or having to add each of them to one or two resource lists, definitely defeats the purpose of reducing effort. My idea was to have a folder tree filled with HTML files and images, and using this module to display them in the RN app as they show up on the accompanying web site without having to do any cross-platform adjustments. Using JSX files (and maybe using/building a converter for the back and forth to HTML) may be the simpler approach after all.

If anyone sees a solution to those snags that I've overlooked, let me know!

pekkanikolaus avatar Jul 22 '17 10:07 pekkanikolaus

Hrm, ok. Will have a wee think about this one :)

bas-ie avatar Jul 22 '17 23:07 bas-ie

I used a similar path:

file:///data/user/0/com.sugoireader/files/gSmWCJF.png

But the image is too blurry 2020-05-17_06-39-56

KVinS avatar May 17 '20 03:05 KVinS