Nodes should ask Fresco to resize images

Summary:
Nodes currently doesn't ask Fresco to resize images, but this is
potentially problematic (ex having a camera photo of 6000x1500 causes a crash
due to the massive size).

Differential Revision: D3687944
This commit is contained in:
Ahmed El-Helw 2016-08-08 18:08:09 -07:00
parent a4c4a88e27
commit 497b02ad30
1 changed files with 30 additions and 4 deletions

View File

@ -19,11 +19,13 @@ import android.graphics.Canvas;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.drawable.ScalingUtils.ScaleType;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.facebook.infer.annotation.Assertions;
@ -42,9 +44,10 @@ import com.facebook.react.views.imagehelper.MultiSourceHelper.MultiSourceResult;
*/
/* package */ final class DrawImageWithDrawee extends AbstractDrawCommand
implements DrawImage, ControllerListener {
private static final String LOCAL_FILE_SCHEME = "file";
private static final String LOCAL_CONTENT_SCHEME = "content";
private final List<ImageSource> mSources = new LinkedList<>();
private @Nullable Context mContext;
private @Nullable DraweeRequestHelper mRequestHelper;
private @Nullable PorterDuffColorFilter mColorFilter;
private ScaleType mScaleType = ImageResizeMode.defaultValue();
@ -79,7 +82,6 @@ import com.facebook.react.views.imagehelper.MultiSourceHelper.MultiSourceResult;
}
}
}
mContext = context;
}
@Override
@ -254,11 +256,23 @@ import com.facebook.react.views.imagehelper.MultiSourceHelper.MultiSourceResult;
mRequestHelper = null;
return;
}
ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(source.getUri()).build();
ResizeOptions resizeOptions = null;
if (shouldResize(source)) {
final int width = (int) (getRight() - getLeft());
final int height = (int) (getBottom() - getTop());
resizeOptions = new ResizeOptions(width, height);
}
ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(source.getUri())
.setResizeOptions(resizeOptions)
.build();
ImageRequest cachedImageRequest = null;
if (cachedSource != null) {
cachedImageRequest = ImageRequestBuilder.newBuilderWithSource(cachedSource.getUri()).build();
cachedImageRequest = ImageRequestBuilder.newBuilderWithSource(cachedSource.getUri())
.setResizeOptions(resizeOptions)
.build();
}
mRequestHelper = new
DraweeRequestHelper(Assertions.assertNotNull(imageRequest), cachedImageRequest, this);
@ -267,4 +281,16 @@ import com.facebook.react.views.imagehelper.MultiSourceHelper.MultiSourceResult;
private boolean shouldDisplayBorder() {
return mBorderColor != 0 || mBorderRadius >= 0.5f;
}
private static boolean shouldResize(ImageSource imageSource) {
// Resizing is inferior to scaling. See http://frescolib.org/docs/resizing-rotating.html
// We resize here only for images likely to be from the device's camera, where the app developer
// has no control over the original size
Uri uri = imageSource.getUri();
String type = uri == null ? null : uri.getScheme();
// one day, we can replace this with what non-Nodes does, which is:
// UriUtil.isLocalContentUri || UriUtil.isLocalFileUri
// not doing this just to save including eyt another BUCK dependency
return LOCAL_FILE_SCHEME.equals(type) || LOCAL_CONTENT_SCHEME.equals(type);
}
}