From 58b0b56a86fc0ade38cdb67c83643c5543823e2b Mon Sep 17 00:00:00 2001 From: Maxime Mongeau Date: Fri, 11 Dec 2020 10:00:36 -0500 Subject: [PATCH] Constraint bitmap ratio to not be lower than zero. Adjust target rectSize to respect ratio --- SDWebImageSVGCoder/Classes/SDImageSVGCoder.m | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/SDWebImageSVGCoder/Classes/SDImageSVGCoder.m b/SDWebImageSVGCoder/Classes/SDImageSVGCoder.m index a44ce44..84328ca 100644 --- a/SDWebImageSVGCoder/Classes/SDImageSVGCoder.m +++ b/SDWebImageSVGCoder/Classes/SDImageSVGCoder.m @@ -200,6 +200,18 @@ - (UIImage *)createBitmapSVGWithData:(nonnull NSData *)data targetSize:(CGSize)t CGFloat xRatio = targetRect.size.width / rect.size.width; CGFloat yRatio = targetRect.size.height / rect.size.height; + + // If we specify only one length of the size (width or height) we want to keep the ratio for that length + if (preserveAspectRatio) { + if (xRatio <= 0) { + xRatio = yRatio; + targetRect.size = CGSizeMake(rect.size.width * xRatio, targetSize.height); + } else if (yRatio <= 0) { + yRatio = xRatio; + targetRect.size = CGSizeMake(targetSize.width, rect.size.height * yRatio); + } + } + CGFloat xScale = preserveAspectRatio ? MIN(xRatio, yRatio) : xRatio; CGFloat yScale = preserveAspectRatio ? MIN(xRatio, yRatio) : yRatio;