Skip to content
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

add image size to onLoad nativeEvent #2364

Conversation

felixakiragreen
Copy link
Contributor

Issue: #494
Related: #2180

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 17, 2015
@paramaggarwal
Copy link
Contributor

Excellent. :)

@felixakiragreen
Copy link
Contributor Author

@paramaggarwal can you tell me why the checks failed? This is my first PR...

@paramaggarwal
Copy link
Contributor

Network failed on the CI server. Try pushing another commit to trigger a new build.

@ide
Copy link
Contributor

ide commented Aug 17, 2015

I think this returns the displayed size, not the intrinsic size. Can you verify?

@hayeah
Copy link
Contributor

hayeah commented Aug 18, 2015

@ide @DUBERT RCTImageView gets the resized dimensions.

Would need to change downloadImageForURL to return the intrinsic image size.

Relevant code:

RCTDispatchCallbackOnMainQueue(completionBlock, error, image);

return [_bridge.imageDownloader downloadImageForURL:url size:size scale:scale resizeMode:resizeMode progressBlock:progressBlock completionBlock:^(NSError *error, id image) {
      RCTDispatchCallbackOnMainQueue(completionBlock, error, image);
    }];

@felixakiragreen
Copy link
Contributor Author

I don't know Objective-C, so here's my best shot. I expect there will be a number of revisions necessary to bring it up to snuff.

I believe there is value in providing both the displayed size and intrinsic image size. So I kept size and added dimensions and scale (so that pixel value can be calculated if wanted).

Because I modified the imageLoader function and callback, I had to update it in a bunch of locations, passing nil where dimensions weren't relevant. I didn't know a better way to approach this.

Thoughts @ide, @hayeah ?

@paramaggarwal
Copy link
Contributor

Wow, I didn't know that RN resizes the image based on the size it will be used at. Quite a nice memory optimisation. My vote is that only dimensions are needed as size is available on onLayout and scale is available using PixelRatio.

@ide
Copy link
Contributor

ide commented Aug 19, 2015

I'm not sure it's worth showing the displayed size because that should be the size of the Image component. Basically what @paramaggarwal said.

@felixakiragreen
Copy link
Contributor Author

Updated to only show size for network images.

@paramaggarwal
Copy link
Contributor

Looks good! Small nitpick - no need for the if (intrinsicSize.height != 0 && intrinsicSize.width != 0) checking - if it 0 it will be sent as 0 - not a problem.

@felixakiragreen
Copy link
Contributor Author

@paramaggarwal I'm checking it so that I don't pass size: { height: 0, width: 0 } for non-network images. That way the user doesn't think there is a bug or something is returning incorrectly.

@paramaggarwal
Copy link
Contributor

@DUBERT I don't think that is a problem. Let's wait for others' thoughts.

@felixakiragreen
Copy link
Contributor Author

pinging @ide @hayeah who needs to review this?

@@ -126,7 +126,7 @@ - (void)reloadImage
scale:RCTScreenScale()
resizeMode:self.contentMode
progressBlock:progressHandler
completionBlock:^(NSError *error, id image) {
completionBlock:^(NSError *error, id image, id dimensions) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NSValue instead of id

@felixakiragreen felixakiragreen force-pushed the network-image-size-onload branch from b4b9c01 to a40f9c1 Compare August 31, 2015 15:19
@felixakiragreen
Copy link
Contributor Author

@ide updated and squashed

@jnhuynh
Copy link

jnhuynh commented Sep 9, 2015

+1

@ide
Copy link
Contributor

ide commented Sep 9, 2015

cc @a2 @nicklockwood for review

@felixakiragreen felixakiragreen force-pushed the network-image-size-onload branch 3 times, most recently from b0ffe88 to fddc263 Compare September 11, 2015 15:26
@felixakiragreen
Copy link
Contributor Author

@paramaggarwal any idea why the checks have failed? You were able to help me last time

@paramaggarwal
Copy link
Contributor

@DUBERT

RCTImageLoaderTests.m:46:33: error: too few arguments to block call, expected 3, have 2
    completionHandler(nil, image);

@felixakiragreen felixakiragreen force-pushed the network-image-size-onload branch from 77e25fe to 8a3731c Compare October 1, 2015 14:14
@felixakiragreen
Copy link
Contributor Author

@ide, @a2, @nicklockwood for review? 😸

@@ -194,7 +194,18 @@ - (void)reloadImage
}
} else {
if (_onLoad) {
_onLoad(nil);
if (dimensions != nil) {
CGSize size = [dimensions CGSizeValue];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we greatly simplify this PR by using image.size (may need to be careful to account for screen scale -- but it sure would make this PR super short :D )?

nvm, that goes back to my original comment on this PR

@felixakiragreen felixakiragreen force-pushed the network-image-size-onload branch from 8a3731c to e9f6791 Compare October 20, 2015 19:12
@facebook-github-bot
Copy link
Contributor

@DUBERT updated the pull request.

@felixakiragreen felixakiragreen force-pushed the network-image-size-onload branch from e9f6791 to e7383fe Compare October 20, 2015 19:31
@facebook-github-bot
Copy link
Contributor

@DUBERT updated the pull request.

@felixakiragreen felixakiragreen force-pushed the network-image-size-onload branch from e7383fe to 1e632f1 Compare October 20, 2015 19:47
add dimensions and scale properties to the nativeEvent

add comments

add check for images without intrinsic size

only show size for network images

clean up formatting

create event first, check for nil

change id dimensions to NSValue *dimensions

change to NSValue *dimensions

catch http images

remove file git included

add tests

fix tests

fix errors

remove console.log
@facebook-github-bot
Copy link
Contributor

@DUBERT updated the pull request.

@felixakiragreen felixakiragreen force-pushed the network-image-size-onload branch from 1e632f1 to 6544806 Compare October 20, 2015 19:49
@facebook-github-bot
Copy link
Contributor

@DUBERT updated the pull request.

@nicklockwood
Copy link
Contributor

I'm not sure I understand the purpose of this. Why not just get the size from the image directly? (image.size). Why do you care how big the image was originally before we resized it? How would this information be used?

@nicklockwood
Copy link
Contributor

OK, sorry, should've read the attached issue properly. I understand the problem, but this is the wrong solution. Loading the image at the wrong size using the <Image> tag and then resizing it to the size you want is pretty hacky, and I don't want to build an API around supporting a hack - if we're going to make API changes then we should support the use case properly, even if it means a bigger change.

I'd be fine with sending the actual loaded image size in the callback. One option would be to do that, then you could load the image at the maximum possible size and then resize it. Still a hack, but at least the API would be logical. You could embed the <Image> inside a smaller view with overflow: hidden, or obscure it behind another view while it's loading to prevent it from messing up the UI before it's resized.

In terms of solving the actual use case, I think we need to add a way to preload images and get their size without having to display the image while its loading. It would be relative easy to expose something like that, but I'll need to discuss with the Android team what the API should look like.

@eyaleizenberg
Copy link

I tried adding these on RN 0.13 and I couldn't get the project to start... 😢

@olofd
Copy link

olofd commented Dec 3, 2015

I really need this fix as well. Is there any work going on here?

@felixakiragreen
Copy link
Contributor Author

@olofd Nope. I don't know how to proceed. It sounds like the FB devs don't like the way I solved this. But it's not a feature they are planning to provide themselves. So right now I'm just having to use a forked version of react-native. I'm several versions behind because I'm tired of rebasing :(

@olofd
Copy link

olofd commented Dec 5, 2015

Ok @DUBERT . for everyone who has a problem with this and want to be able to support remote images with unknown dimensions I made a hack you can use before the react team supports the use-case.

This branch is not suppose to be beautiful, rather to touch as little as possible, so it'll be easy to keep merging, and still give you the functionality.
(Decided to not use your PR as base @DUBERT because it was so 'correct' 😄 , and thereby makes it harder to merge/rebase)

It's based on 0.16.0:
https://github.com/olofd/react-native/tree/Remote-Image-Dimensions

Compare against 0.16.0:
https://github.com/facebook/react-native/compare/0.16-stable...olofd:Remote-Image-Dimensions?expand=1

To use it. Just change your package.json react-native-version to:
"react-native": "git://github.com/olofd/react-native.git#Remote-Image-Dimensions"

I'll try to keep merging it with new releases.

@felixakiragreen
Copy link
Contributor Author

That looks great @olofd, I'll use it in my project.

@nicklockwood Do you have any more thoughts about this PR? Should I go ahead and close it? Or is it worth trying to figure out how to preload images and get their size? Please keep in mind I have very little Objective-C experience.

@nicklockwood
Copy link
Contributor

I'm still actively investigating the right API for preloading. I don't want to add something we won't be able to support moving forward. Sorry about the wait.

@olofd
Copy link

olofd commented Dec 12, 2015

Updated my branch with this feature for v0.17.0-rc:
https://github.com/olofd/react-native/tree/Image-Dimensions-0.17.0-rc

Use this in package.json:
"react-native": "git://github.com/olofd/react-native.git#Image-Dimensions-0.17.0-rc"

Compare-view:
https://github.com/facebook/react-native/compare/0.17-stable...olofd:Image-Dimensions-0.17.0-rc?expand=1

@onchainguy-btc
Copy link
Contributor

@olofd using your branch, too. Works like a charm 👍

@onchainguy-btc
Copy link
Contributor

@olofd do you have any experience on using this with Android? Seems like the events are not fired there (#4807)

@olofd
Copy link

olofd commented Dec 20, 2015

Updated my branch with this feature for v0.17.0:
https://github.com/olofd/react-native/tree/Image-Dimensions-0.17.0

Use this in package.json:
"react-native": "git://github.com/olofd/react-native.git#Image-Dimensions-0.17.0"

@felixakiragreen felixakiragreen deleted the network-image-size-onload branch March 2, 2016 17:25
@dbuarque
Copy link

So, what's the deal?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.