From 7ddbf2a73b1a5ace952343aefff33d7f9b511e4f Mon Sep 17 00:00:00 2001 From: Jeremy Smereka Date: Thu, 30 Nov 2017 14:32:17 -0700 Subject: [PATCH] Fixes Android Release images not rendering (Issue #129) (#514) * Fixes Android Release images not rendering (Issue #129) (#1) * Fixes Android Release images not rendering (Issue #129) Issue caused by 2 things: - Image resources were not properly getting the path due to missing scheme (if required via require('./images/img.png')) - markUpdated() was not causing a re-render thus image was not updating if it was a resource id after fetching. This is been changed to directly call bitmapTryRender, however, this may not be the best approach to this issue. Minor note: After making this change, RenderableShadowNode would get a null for images. I put a null check for the alpha parameter to suppress this. * Reverts check for color work around --- .../java/com/horcrux/svg/ImageShadowNode.java | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/android/src/main/java/com/horcrux/svg/ImageShadowNode.java b/android/src/main/java/com/horcrux/svg/ImageShadowNode.java index 13777e27..8aa92e53 100644 --- a/android/src/main/java/com/horcrux/svg/ImageShadowNode.java +++ b/android/src/main/java/com/horcrux/svg/ImageShadowNode.java @@ -32,6 +32,7 @@ import com.facebook.imagepipeline.request.ImageRequestBuilder; import com.facebook.react.bridge.ReadableMap; import com.facebook.react.common.ReactConstants; import com.facebook.react.uimanager.annotations.ReactProp; +import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper; import java.util.concurrent.atomic.AtomicBoolean; @@ -105,6 +106,9 @@ class ImageShadowNode extends RenderableShadowNode { mImageHeight = 0; } mUri = Uri.parse(uriString); + if (mUri.getScheme() == null) { + mUri = ResourceDrawableIdHelper.getInstance().getResourceDrawableUri(getThemedContext(), uriString); + } } } @@ -148,7 +152,7 @@ class ImageShadowNode extends RenderableShadowNode { if (Fresco.getImagePipeline().isInBitmapMemoryCache(request)) { tryRender(request, canvas, paint, opacity * mOpacity); } else { - loadBitmap(request); + loadBitmap(request, canvas, paint, opacity * mOpacity); } } } @@ -160,16 +164,14 @@ class ImageShadowNode extends RenderableShadowNode { return path; } - private void loadBitmap(ImageRequest request) { + private void loadBitmap(ImageRequest request, final Canvas canvas, final Paint paint, final float opacity) { final DataSource> dataSource = Fresco.getImagePipeline().fetchDecodedImage(request, getThemedContext()); - dataSource.subscribe(new BaseBitmapDataSubscriber() { @Override public void onNewResultImpl(Bitmap bitmap) { mLoading.set(false); - SvgViewShadowNode shadowNode = getSvgShadowNode(); - shadowNode.markUpdated(); + bitmapTryRender(bitmap, canvas, paint, opacity * mOpacity); } @Override @@ -284,4 +286,14 @@ class ImageShadowNode extends RenderableShadowNode { dataSource.close(); } } + + private void bitmapTryRender(Bitmap bitmap, Canvas canvas, Paint paint, float opacity) { + try { + if (bitmap != null) { + doRender(canvas, paint, bitmap, opacity); + } + } catch (Exception e) { + throw new IllegalStateException(e); + } + } }