From 8ce13403bc13e1b0aede614328228a6f64017db7 Mon Sep 17 00:00:00 2001 From: ty0x2333 Date: Wed, 26 Jan 2022 07:12:35 +0800 Subject: [PATCH 1/2] Fix the bug that `decodedAndScaledDownImageWithImage` does not render the image completely due to the loss of precision. --- SDWebImage/Core/SDImageCoderHelper.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SDWebImage/Core/SDImageCoderHelper.m b/SDWebImage/Core/SDImageCoderHelper.m index 321710b5..95a95000 100644 --- a/SDWebImage/Core/SDImageCoderHelper.m +++ b/SDWebImage/Core/SDImageCoderHelper.m @@ -448,7 +448,7 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over float dify = destTile.size.height; destTile.size.height = CGImageGetHeight( sourceTileImageRef ) * imageScale; dify -= destTile.size.height; - destTile.origin.y += dify; + destTile.origin.y = MIN(0, destTile.origin.y + dify); } CGContextDrawImage( destContext, destTile, sourceTileImageRef ); CGImageRelease( sourceTileImageRef ); From 33ff352d4ecc2cf05ffae0e6378615f85c1e3c98 Mon Sep 17 00:00:00 2001 From: ty0x2333 Date: Wed, 26 Jan 2022 15:46:21 +0800 Subject: [PATCH 2/2] Add test case: test07ThatDecodeAndScaleDownAlwaysCompleteRendering --- Tests/Tests/SDImageCoderTests.m | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Tests/Tests/SDImageCoderTests.m b/Tests/Tests/SDImageCoderTests.m index ee2614d6..199c92be 100644 --- a/Tests/Tests/SDImageCoderTests.m +++ b/Tests/Tests/SDImageCoderTests.m @@ -96,6 +96,25 @@ expect(decodedImage.size.height).to.equal(1); } +- (void)test07ThatDecodeAndScaleDownAlwaysCompleteRendering { + // Check that when the height of the image used is not evenly divisible by the height of the tile, the output image can also be rendered completely. + + UIColor *imageColor = UIColor.blackColor; + CGSize imageSize = CGSizeMake(3024, 4032); + CGRect imageRect = CGRectMake(0, 0, imageSize.width, imageSize.height); + SDGraphicsImageRendererFormat *format = [[SDGraphicsImageRendererFormat alloc] init]; + format.scale = 1; + SDGraphicsImageRenderer *renderer = [[SDGraphicsImageRenderer alloc] initWithSize:imageSize format:format]; + UIImage *image = [renderer imageWithActions:^(CGContextRef _Nonnull context) { + CGContextSetFillColorWithColor(context, [imageColor CGColor]); + CGContextFillRect(context, imageRect); + }]; + + UIImage *decodedImage = [UIImage sd_decodedAndScaledDownImageWithImage:image limitBytes:20 * 1024 * 1024]; + UIColor *testColor = [decodedImage sd_colorAtPoint:CGPointMake(0, decodedImage.size.height - 1)]; + expect(testColor.sd_hexString).equal(imageColor.sd_hexString); +} + - (void)test08ThatEncodeAlphaImageToJPGWithBackgroundColor { NSString *testImagePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"TestImage" ofType:@"png"]; UIImage *image = [[UIImage alloc] initWithContentsOfFile:testImagePath];