From f5b88d5f420bc65f3f8ab16d03a0ff6cb2f4646f Mon Sep 17 00:00:00 2001 From: David Boho Date: Tue, 10 Jul 2018 15:00:25 +0200 Subject: [PATCH] fix some OOM, do not upscale images https://github.com/apache/cordova-plugin-camera/pull/238 --- src/android/CameraLauncher.java | 7 ++++--- src/ios/UIImage+CropScaleOrientation.m | 8 ++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/android/CameraLauncher.java b/src/android/CameraLauncher.java index b80c46090..805af3cf9 100644 --- a/src/android/CameraLauncher.java +++ b/src/android/CameraLauncher.java @@ -1064,7 +1064,6 @@ private Bitmap getScaledAndRotatedBitmap(String imageUrl, boolean unknownSources // determine the correct aspect ratio int[] widthHeight = calculateAspectRatio(rotatedWidth, rotatedHeight); - // Load in the smallest bitmap possible that is closest to the size we want options.inJustDecodeBounds = false; options.inSampleSize = calculateSampleSize(rotatedWidth, rotatedHeight, widthHeight[0], widthHeight[1]); @@ -1122,8 +1121,10 @@ private Bitmap getScaledAndRotatedBitmap(String imageUrl, boolean unknownSources * @return */ public int[] calculateAspectRatio(int origWidth, int origHeight) { - int newWidth = this.targetWidth; - int newHeight = this.targetHeight; + // fix some OOM + // do not upscale image + int newWidth = Math.min(this.targetWidth, origWidth); + int newHeight = Math.min(this.targetHeight, origHeight); // If no new width or height were specified return the original bitmap if (newWidth <= 0 && newHeight <= 0) { diff --git a/src/ios/UIImage+CropScaleOrientation.m b/src/ios/UIImage+CropScaleOrientation.m index 63e044f2f..39c0e1331 100644 --- a/src/ios/UIImage+CropScaleOrientation.m +++ b/src/ios/UIImage+CropScaleOrientation.m @@ -28,8 +28,8 @@ - (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize CGSize imageSize = sourceImage.size; CGFloat width = imageSize.width; CGFloat height = imageSize.height; - CGFloat targetWidth = targetSize.width; - CGFloat targetHeight = targetSize.height; + CGFloat targetWidth = MIN(targetSize.width, width); + CGFloat targetHeight = MIN(targetSize.height, height); CGFloat scaleFactor = 0.0; CGFloat scaledWidth = targetWidth; CGFloat scaledHeight = targetHeight; @@ -136,8 +136,8 @@ - (UIImage*)imageByScalingNotCroppingForSize:(CGSize)targetSize CGSize imageSize = sourceImage.size; CGFloat width = imageSize.width; CGFloat height = imageSize.height; - CGFloat targetWidth = targetSize.width; - CGFloat targetHeight = targetSize.height; + CGFloat targetWidth = MIN(targetSize.width, width); + CGFloat targetHeight = MIN(targetSize.height, height); CGFloat scaleFactor = 0.0; CGSize scaledSize = targetSize;