Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import coil.decode.SvgDecoder
import coil.request.CachePolicy
import coil.request.ImageRequest
import coil.request.SuccessResult
import coil.size.Precision
import coil.size.Scale
import com.facebook.react.bridge.ReactApplicationContext
import com.jimmydaddy.imagemarker.base.Constants.IMAGE_MARKER_TAG
Expand Down Expand Up @@ -144,15 +145,21 @@ class MarkerImageLoader(private val context: ReactApplicationContext, private va
// The returned bitmap is explicitly recycled after rendering. Keep it out of Coil's
// memory cache so that recycling cannot invalidate a shared cached drawable.
.memoryCachePolicy(CachePolicy.DISABLED)
val requestedSize = if (img.src.width > 0 && img.src.height > 0) {
val hasSourceSize = img.src.width > 0 && img.src.height > 0
val requestedSize = if (hasSourceSize) {
ImageSizeLimiter.fit(img.src.width, img.src.height, maxSize)
} else {
ImagePixelSize(maxSize, maxSize)
}
request = request
.size(requestedSize.width, requestedSize.height)
.scale(Scale.FIT)
if (img.src.width > 0 && img.src.height > 0) {
if (!hasSourceSize) {
// Without a source size, maxSize x maxSize is only an upper bound. Coil defaults to exact
// precision for requests without a view, which would scale smaller images up to fill it.
request = request.precision(Precision.INEXACT)
}
if (hasSourceSize) {
Log.d(IMAGE_MARKER_TAG, "src.width: " + img.src.width + " src.height: " + img.src.height)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ class MarkerImageLoaderTest {
bitmap.recycle()
}

@Test
fun doesNotUpscaleCoilSourceWithoutExplicitSize() = runBlocking {
val uri = "content://com.imagemarkerexample.test.imageprovider/image"

// maxSize is an upper bound: a 3x2 source must not be enlarged to fit 2048x2048.
val bitmap = load(uri)

assertEquals(3, bitmap.width)
assertEquals(2, bitmap.height)
bitmap.recycle()
}

@Test
fun loadsAndroidResourceUriThroughCoil() = runBlocking {
val uri = "android.resource://${targetContext.packageName}/${R.mipmap.ic_launcher}"
Expand Down