-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Image] Set correct image height #494
Comments
Unfortunately CSS doesn't have a good way to do aspect ratio like this. The best hack you can come up with is padding-bottom: 150% but it's super ugly. I prototyped an implementation of aspectRatio and it worked but wasn't spec compliant so I backed off. In the meantime, you can do height: require('Dimensions').get('window').width / 1.5 |
If possible, could you show your aspectRatio prototype implantation? |
Same concern here. Is there a way to get the width of the parent view? Then I can do the math and set absolute height and width on the Image. The parent view itself might be configured with |
@paramaggarwal - check out these bridge methods on RCTUIManager for performing measurements |
Please feel free to close this. Continuing discussion and sharing code snippets on #858. |
This is what I need. I don't know the width and height before I receive the image. Is there a way to do this @brentvatne ? |
@DUBERT - there is no way to do this right now, at least as far as I know, but you could potentially submit a patch to provide this information in a load callback at some point. For example, you can add the following code to RCTImageDownloader to see the dimensions logged to your Xcode console:
I'm curious - what are your use cases for this? |
@brentvatne I have a listview with rows of flexible height. Some rows have network images, and I want to make them full width, and then have their height proportionally take up as much space as they need. So I was thinking.. if I could get the height and width, then I could calculate how height it should be and set it. Because right now, if a static height is not set on the image, then it doesn’t show. But each one will be different, and I don't have it before hand. |
@DUBERT You'll see the items stutter around if you implement something like that, because let's say the third image loads first and takes up its space. Then the second one loads and pushes the third one down. Now imagine this constant re-layout for tens of items. Ideally you would want to let your server tell the app of the image |
I can handle the stuttering, that's what I want: the image size in the onLoad as a callback. Because then at least I could have a progress bar, and then animated it to it's full height once it's loaded. I've been digging around the RCT Image library but I don't know any Objective-C. :( |
Progress indicator is available: https://github.com/oblador/react-native-image-progress, but image size data is not exposed. Maybe you could start a separate issue to generate interest in implementing the feature? What you are looking for is to send
and then read it on |
@paramaggarwal Thank you very much for your assistance. One last issue if you're willing to help out. I'm using 0.8.0, and the source looks pretty different for it. Would I add that event here? https://github.com/facebook/react-native/blob/0.8-stable/Libraries/Image/RCTNetworkImageView.m#L123-L124 Doesn't the image need to be passed as a parameter? I tried using master, but there were many errors in Xcode, I don't know if I'm supposed to build it or something :( |
@DUBERT Ah you are right. Here is what that entire section will look like: Add this right under
and then call If it works, please do raise a PR! (for the version on master) This is useful. |
Woohoo! I got it working, thanks so much @paramaggarwal. I'll try to get master working on my machine, so I can submit a PR for it. |
I look forward to this PR! :) 👍 |
I'm having trouble implementing this. The size that gets returned across the bridge is the size that I had specified in the styles all of the time? Skipping the size declaration results in no image being loaded though. Could either of you shed any further light on this? @DUBERT, @paramaggarwal ? |
Further testing shows that it's not always the defined sizes that get returned. The sizes vary depending on the |
Thanks, @paramaggarwal. I think I may have just found it. The image must have resizeMode 'contain' applied and also the sizes returned are scaled in proportion to the device dpi, e.g. iPhone6 returns 50/100 for a 100/200 image. Thanks for the code above. I think the only place it fails is for gif's but that's due to the alternate loading method. |
Extra info for anyone else.. the style attributed to the e.g. loading a 300/200 image yields the following results. (Container Size vs reported image size)
Wrapping a large image window inside a 1*1 view with |
I've published to npm react-native-fit-image. It enables you to draw responsive image component. |
I want to display images from the internet and want to make them as wide as the parent view, which is possible by setting the flex value. But I also want to make the image as high as width * aspectRatio.
Neither StyleSheet.create nor the direct style tag gives access to the image width and aspectRatio.
Any ideas?
The text was updated successfully, but these errors were encountered: