Include image dimensions & url in Image's onLoad callback

Summary:
This is similar to the iOS feature that was implemented by 84f68c338a.

**Test plan (required)**

Verified that the image dimensions are included in the `onLoad` callback in a test app. Also, this change is used in my team's app.

Adam Comella
Microsoft Corp.
Closes https://github.com/facebook/react-native/pull/9608

Differential Revision: D3797659

Pulled By: foghina

fbshipit-source-id: ea83a907bf6b0b61d9bc1e90fc7c64b7132db81f
This commit is contained in:
Adam Comella 2016-09-01 02:38:37 -07:00 committed by Facebook Github Bot 9
parent 1e5d52bf0b
commit bcf48e74ae
2 changed files with 31 additions and 5 deletions

View File

@ -36,18 +36,28 @@ public class ImageLoadEvent extends Event<ImageLoadEvent> {
private final int mEventType;
private final @Nullable String mImageUri;
private final int mWidth;
private final int mHeight;
public ImageLoadEvent(int viewId, @ImageEventType int eventType) {
this(viewId, eventType, null);
}
public ImageLoadEvent(int viewId, @ImageEventType int eventType, String imageUri) {
this(viewId, eventType, imageUri, 0, 0);
}
public ImageLoadEvent(
int viewId,
@ImageEventType int eventType,
@Nullable String imageUri) {
@Nullable String imageUri,
int width,
int height) {
super(viewId);
mEventType = eventType;
mImageUri = imageUri;
mWidth = width;
mHeight = height;
}
public static String eventNameForType(@ImageEventType int eventType) {
@ -82,10 +92,25 @@ public class ImageLoadEvent extends Event<ImageLoadEvent> {
@Override
public void dispatch(RCTEventEmitter rctEventEmitter) {
WritableMap eventData = null;
if (mImageUri != null) {
if (mImageUri != null || mEventType == ON_LOAD) {
eventData = Arguments.createMap();
if (mImageUri != null) {
eventData.putString("uri", mImageUri);
}
if (mEventType == ON_LOAD) {
WritableMap source = Arguments.createMap();
source.putDouble("width", mWidth);
source.putDouble("height", mHeight);
if (mImageUri != null) {
source.putString("url", mImageUri);
}
eventData.putMap("source", source);
}
}
rctEventEmitter.receiveEvent(getViewTag(), getEventName(), eventData);
}
}

View File

@ -201,7 +201,8 @@ public class ReactImageView extends GenericDraweeView {
@Nullable Animatable animatable) {
if (imageInfo != null) {
mEventDispatcher.dispatchEvent(
new ImageLoadEvent(getId(), ImageLoadEvent.ON_LOAD));
new ImageLoadEvent(getId(), ImageLoadEvent.ON_LOAD,
mImageSource.getSource(), imageInfo.getWidth(), imageInfo.getHeight()));
mEventDispatcher.dispatchEvent(
new ImageLoadEvent(getId(), ImageLoadEvent.ON_LOAD_END));
}