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

[Image] Resize mode contain produces glitches when dimension is float

Open grabbou opened this issue 11 years ago • 11 comments

Steps to reproduce:
  1. Load image over HTTP with following size:
    • width: 350
    • height: 460
  2. Attach it to a styled Image element with:
    • width: 200
    • height: 200
    • resizeMode: contain
  3. Run - https://rnplay.org/apps/Z4KTfg

Output: screen shot 2015-07-04 at 20 16 08

Image is resized and centred correctly, but because target is wider then the content, width is calculated like so:

sourceSize.width = sourceSize.height * aspect;

This makes the width to be a float 152.17391304347828 and causes a grey (I think 1px) border to appear at the right of the image. If I manipulate the content size, the grey border may appear at the bottom respectively.

[Update] Possible fixes

Ok, so I've played around with this for a while to see what's exactly causing this behaviour.

Commenting these lines https://github.com/facebook/react-native/blob/master/Libraries/Image/RCTImageDownloader.m#L145-L173 solves the problem, so the issue lives there (Yay, CSS & LayoutConstraints for the win!)

The calculated boundaries should be rounded to avoid above artefacts from happening by either:

  • explicitly rounding every calculation
  • passing calculated imageRect through CGRectIntegral which will require less modifications as there are multiple return points from RCTClipRect. Not a huge fun of it though as in my understanding, RCTClipRect should return already adjusted values w/o further manipulation, but anyway worth discussing. Below is a simple demonstration how line 149 can be modified from this:
CGRect imageRect = RCTClipRect(image.size, image.scale, size, destScale, resizeMode);

to this:

CGRect imageRect = CGRectIntegral(RCTClipRect(image.size, image.scale, size, destScale, resizeMode));

If that's something that matches one of the possible ways of fixing this solution, I can submit a PR.

grabbou avatar Jul 04 '15 18:07 grabbou

I agree that it would be better if the solution was contained within RCTClipRect. Ideally it would also take destScale factor into account, so that if the scale was 2.0, it would round to the nearest 0.5 instead of the nearest integer, if that makes sense.

nicklockwood avatar Jul 07 '15 15:07 nicklockwood

That's actually a good point about the scale factor. I can submit a PR and we can move discussion over there.

grabbou avatar Jul 07 '15 15:07 grabbou

Sounds good, thanks!

nicklockwood avatar Jul 07 '15 15:07 nicklockwood

Just to let you know, I'll be working on that tomorrow (we already have fix in our forked temporary version we use to test it out before submitting).

grabbou avatar Jul 17 '15 21:07 grabbou

:+1:

michaelcontento avatar Jul 21 '15 10:07 michaelcontento

good job :thumbsup:

cssoul avatar Jul 25 '15 04:07 cssoul

I added some fixes for this in RCTClipRect, so hopefully images should be OK now. My fixes won't affect text or border drawing though.

nicklockwood avatar Jul 25 '15 08:07 nicklockwood

@nicklockwood, If that's the case, do you mean #2018 needs another standalone fix?

ssssssssssss avatar Jul 28 '15 10:07 ssssssssssss

Yes, or at least I haven't done any work to fix that (it may have been fixed by someone else).

nicklockwood avatar Jul 28 '15 10:07 nicklockwood

Thanks. :thumbsup:

ssssssssssss avatar Jul 28 '15 10:07 ssssssssssss

@brentvatne @nicklockwood the rnplay example has no glitches on React 0.11. Closing, thanks!

grabbou avatar Sep 28 '15 15:09 grabbou