diff --git a/SDWebImage/Core/SDImageIOAnimatedCoder.m b/SDWebImage/Core/SDImageIOAnimatedCoder.m index f5d33a04..a543ac57 100644 --- a/SDWebImage/Core/SDImageIOAnimatedCoder.m +++ b/SDWebImage/Core/SDImageIOAnimatedCoder.m @@ -184,10 +184,10 @@ static CGImageRef SDImageIOPNGPluginBuggyCreateWorkaround(CGImageRef cgImage) CF CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(cgImage); CGImageAlphaInfo alphaInfo = (bitmapInfo & kCGBitmapAlphaInfoMask); CGImageAlphaInfo newAlphaInfo = alphaInfo; - if (alphaInfo == kCGImageAlphaPremultipliedLast) { - newAlphaInfo = kCGImageAlphaLast; - } else if (alphaInfo == kCGImageAlphaPremultipliedFirst) { - newAlphaInfo = kCGImageAlphaFirst; + if (alphaInfo == kCGImageAlphaLast) { + newAlphaInfo = kCGImageAlphaPremultipliedLast; + } else if (alphaInfo == kCGImageAlphaFirst) { + newAlphaInfo = kCGImageAlphaPremultipliedFirst; } if (newAlphaInfo != alphaInfo) { CGBitmapInfo byteOrderInfo = bitmapInfo & kCGBitmapByteOrderMask; @@ -244,6 +244,8 @@ static BOOL SDImageIOPNGPluginBuggyNeedWorkaround(void) { #endif } } + CFRelease(source); + CGImageRelease(cgImage); }); return isBuggy; diff --git a/Tests/SDWebImage Tests.xcodeproj/project.pbxproj b/Tests/SDWebImage Tests.xcodeproj/project.pbxproj index c5746e28..862d94a5 100644 --- a/Tests/SDWebImage Tests.xcodeproj/project.pbxproj +++ b/Tests/SDWebImage Tests.xcodeproj/project.pbxproj @@ -47,6 +47,9 @@ 326E69482334C0C300B7252C /* TestLoopCount.gif in Resources */ = {isa = PBXBuildFile; fileRef = 326E69462334C0C200B7252C /* TestLoopCount.gif */; }; 327054E2206CEFF3006EA328 /* TestImageAnimated.apng in Resources */ = {isa = PBXBuildFile; fileRef = 327054E1206CEFF3006EA328 /* TestImageAnimated.apng */; }; 327054E3206CEFF3006EA328 /* TestImageAnimated.apng in Resources */ = {isa = PBXBuildFile; fileRef = 327054E1206CEFF3006EA328 /* TestImageAnimated.apng */; }; + 3278F5E22B04C1AC0004A6EE /* IndexedPNG.png in Resources */ = {isa = PBXBuildFile; fileRef = 3278F5E12B04C1AC0004A6EE /* IndexedPNG.png */; }; + 3278F5E32B04C1AC0004A6EE /* IndexedPNG.png in Resources */ = {isa = PBXBuildFile; fileRef = 3278F5E12B04C1AC0004A6EE /* IndexedPNG.png */; }; + 3278F5E42B04C1AC0004A6EE /* IndexedPNG.png in Resources */ = {isa = PBXBuildFile; fileRef = 3278F5E12B04C1AC0004A6EE /* IndexedPNG.png */; }; 327A418C211D660600495442 /* TestImage.heic in Resources */ = {isa = PBXBuildFile; fileRef = 327A418B211D660600495442 /* TestImage.heic */; }; 327A418D211D660600495442 /* TestImage.heic in Resources */ = {isa = PBXBuildFile; fileRef = 327A418B211D660600495442 /* TestImage.heic */; }; 328BB6DD20825E9800760D6C /* SDWebImageTestCache.m in Sources */ = {isa = PBXBuildFile; fileRef = 328BB6DC20825E9800760D6C /* SDWebImageTestCache.m */; }; @@ -149,6 +152,7 @@ 3264FF2E205D42CB00F6BD48 /* SDWebImageTestTransformer.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDWebImageTestTransformer.m; sourceTree = ""; }; 326E69462334C0C200B7252C /* TestLoopCount.gif */ = {isa = PBXFileReference; lastKnownFileType = image.gif; path = TestLoopCount.gif; sourceTree = ""; }; 327054E1206CEFF3006EA328 /* TestImageAnimated.apng */ = {isa = PBXFileReference; lastKnownFileType = file; path = TestImageAnimated.apng; sourceTree = ""; }; + 3278F5E12B04C1AC0004A6EE /* IndexedPNG.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = IndexedPNG.png; sourceTree = ""; }; 327A418B211D660600495442 /* TestImage.heic */ = {isa = PBXFileReference; lastKnownFileType = file; path = TestImage.heic; sourceTree = ""; }; 328BAF262240C08E00FC70DD /* Test-Shared.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = "Test-Shared.xcconfig"; sourceTree = ""; }; 328BAF272240C08E00FC70DD /* Test-Release.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = "Test-Release.xcconfig"; sourceTree = ""; }; @@ -266,6 +270,7 @@ isa = PBXGroup; children = ( 32648066250232F7004FA0FC /* 1@2x.gif */, + 3278F5E12B04C1AC0004A6EE /* IndexedPNG.png */, 433BBBBA1D7EFA8B0086B6E9 /* MonochromeTestImage.jpg */, 324047432271956F007C53E1 /* TestEXIF.png */, 3264CD162AAB1E23001E338B /* TestJFIF.jpg */, @@ -497,6 +502,7 @@ 329922892365DC6C00EAFD97 /* TestImageLarge.jpg in Resources */, 32648069250232F7004FA0FC /* 1@2x.gif in Resources */, 3299228A2365DC6C00EAFD97 /* TestImage.png in Resources */, + 3278F5E42B04C1AC0004A6EE /* IndexedPNG.png in Resources */, 329922842365DC6C00EAFD97 /* MonochromeTestImage.jpg in Resources */, 329922882365DC6C00EAFD97 /* TestImage.jpg in Resources */, 32515F9E24AF1919005E8F79 /* TestImageAnimated.webp in Resources */, @@ -524,6 +530,7 @@ 32B99EA6203B31360017FD66 /* TestImage.png in Resources */, 32648068250232F7004FA0FC /* 1@2x.gif in Resources */, 3297A0A023374D1700814590 /* TestImageAnimated.heic in Resources */, + 3278F5E32B04C1AC0004A6EE /* IndexedPNG.png in Resources */, 32B99EA2203B31360017FD66 /* MonochromeTestImage.jpg in Resources */, 32905E65211D786E00460FCF /* TestImage.heif in Resources */, 32515F9D24AF1919005E8F79 /* TestImageAnimated.webp in Resources */, @@ -551,6 +558,7 @@ 433BBBB71D7EF8200086B6E9 /* TestImage.gif in Resources */, 32648067250232F7004FA0FC /* 1@2x.gif in Resources */, 433BBBB91D7EF8260086B6E9 /* TestImage.png in Resources */, + 3278F5E22B04C1AC0004A6EE /* IndexedPNG.png in Resources */, 3297A09F23374D1700814590 /* TestImageAnimated.heic in Resources */, 327054E2206CEFF3006EA328 /* TestImageAnimated.apng in Resources */, 32515F9C24AF1919005E8F79 /* TestImageAnimated.webp in Resources */, diff --git a/Tests/Tests/Images/IndexedPNG.png b/Tests/Tests/Images/IndexedPNG.png new file mode 100644 index 00000000..8ed105e1 Binary files /dev/null and b/Tests/Tests/Images/IndexedPNG.png differ diff --git a/Tests/Tests/SDImageCoderTests.m b/Tests/Tests/SDImageCoderTests.m index 50a70c47..acd70e70 100644 --- a/Tests/Tests/SDImageCoderTests.m +++ b/Tests/Tests/SDImageCoderTests.m @@ -558,6 +558,21 @@ expect(exifOrientation).equal(kCGImagePropertyOrientationDown); } +- (void)test30ThatImageIOPNGPluginBuggyWorkaround { + // See: #3634 + NSURL *url = [[NSBundle bundleForClass:[self class]] URLForResource:@"IndexedPNG" withExtension:@"png"]; + NSData *data = [NSData dataWithContentsOfURL:url]; + + UIImage *decodedImage = [SDImageIOCoder.sharedCoder decodedImageWithData:data options:nil]; + UIColor *testColor1 = [decodedImage sd_colorAtPoint:CGPointMake(100, 1)]; + CGFloat r1, g1, b1, a1; + [testColor1 getRed:&r1 green:&g1 blue:&b1 alpha:&a1]; + expect(r1).beCloseToWithin(0.60, 0.01); + expect(g1).beCloseToWithin(0.91, 0.01); + expect(b1).beCloseToWithin(0.91, 0.01); + expect(a1).beCloseToWithin(0.20, 0.01); +} + #pragma mark - Utils - (void)verifyCoder:(id)coder