From e665fb52d60c2c5fee1c99e70bbc48539eee8cb0 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 1 Jul 2024 18:21:01 +0800 Subject: [PATCH 1/3] Fix the issue that SDAnimatedImage breaks the vector on macOS --- SDWebImage/Core/SDAnimatedImage.h | 2 +- SDWebImage/Core/SDAnimatedImage.m | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/SDWebImage/Core/SDAnimatedImage.h b/SDWebImage/Core/SDAnimatedImage.h index 78739c57..4f596997 100644 --- a/SDWebImage/Core/SDAnimatedImage.h +++ b/SDWebImage/Core/SDAnimatedImage.h @@ -72,7 +72,7 @@ // This class override these methods from UIImage(NSImage), and it supports NSSecureCoding. // You should use these methods to create a new animated image. Use other methods just call super instead. -// @note Before 5.19, these initializer will return nil for static image (when all candidate SDAnimatedImageCoder returns nil instance), like JPEG data. After 5.19, these initializer will retry for static image as well, so JPEG data will return non-nil instance. +// @note Before 5.19, these initializer will return nil for static image (when all candidate SDAnimatedImageCoder returns nil instance), like JPEG data. After 5.19, these initializer will retry for static image as well, so JPEG data will return non-nil instance. For vector image(PDF/SVG), always return nil. // @note When the animated image frame count <= 1, all the `SDAnimatedImageProvider` protocol methods will return nil or 0 value, you'd better check the frame count before usage and keep fallback. + (nullable instancetype)imageNamed:(nonnull NSString *)name; // Cache in memory, no Asset Catalog support #if __has_include() diff --git a/SDWebImage/Core/SDAnimatedImage.m b/SDWebImage/Core/SDAnimatedImage.m index e7467c47..56c63e1c 100644 --- a/SDWebImage/Core/SDAnimatedImage.m +++ b/SDWebImage/Core/SDAnimatedImage.m @@ -141,6 +141,12 @@ static CGFloat SDImageScaleFromPath(NSString *string) { if (!data || data.length == 0) { return nil; } + // Vector image does not supported, guard firstly + SDImageFormat format = [NSData sd_imageFormatForImageData:data]; + if (format == SDImageFormatSVG || format == SDImageFormatPDF) { + return nil; + } + id animatedCoder = nil; SDImageCoderMutableOptions *mutableCoderOptions; if (options != nil) { @@ -167,6 +173,10 @@ static CGFloat SDImageScaleFromPath(NSString *string) { if (!image) { return nil; } + // Vector image does not supported, guard secondly + if (image.sd_isVector) { + return nil; + } #if SD_MAC self = [super initWithCGImage:image.CGImage scale:MAX(scale, 1) orientation:kCGImagePropertyOrientationUp]; #else From bcbe72a77f1357b81f6db91124f0c883c458c998 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 1 Jul 2024 18:21:14 +0800 Subject: [PATCH 2/3] Added test case about SVG image --- .../SDWebImage Tests.xcodeproj/project.pbxproj | 10 ++++++++++ Tests/Tests/Images/TestImage.svg | 4 ++++ Tests/Tests/SDImageCoderTests.m | 17 +++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 Tests/Tests/Images/TestImage.svg diff --git a/Tests/SDWebImage Tests.xcodeproj/project.pbxproj b/Tests/SDWebImage Tests.xcodeproj/project.pbxproj index a12699cb..adee4150 100644 --- a/Tests/SDWebImage Tests.xcodeproj/project.pbxproj +++ b/Tests/SDWebImage Tests.xcodeproj/project.pbxproj @@ -125,6 +125,10 @@ 3299228D2365DC6C00EAFD97 /* TestImageAnimated.apng in Resources */ = {isa = PBXBuildFile; fileRef = 327054E1206CEFF3006EA328 /* TestImageAnimated.apng */; }; 3299228E2365DC6C00EAFD97 /* TestImageAnimated.heic in Resources */ = {isa = PBXBuildFile; fileRef = 3297A09E23374D1600814590 /* TestImageAnimated.heic */; }; 32A571562037DB2D002EDAAE /* SDAnimatedImageTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 32A571552037DB2D002EDAAE /* SDAnimatedImageTest.m */; }; + 32B4A4802C082A160004E42C /* TestImage.svg in Resources */ = {isa = PBXBuildFile; fileRef = 32B4A47F2C082A160004E42C /* TestImage.svg */; }; + 32B4A4812C082A160004E42C /* TestImage.svg in Resources */ = {isa = PBXBuildFile; fileRef = 32B4A47F2C082A160004E42C /* TestImage.svg */; }; + 32B4A4822C082A160004E42C /* TestImage.svg in Resources */ = {isa = PBXBuildFile; fileRef = 32B4A47F2C082A160004E42C /* TestImage.svg */; }; + 32B4A4832C082A160004E42C /* TestImage.svg in Resources */ = {isa = PBXBuildFile; fileRef = 32B4A47F2C082A160004E42C /* TestImage.svg */; }; 32B99E8B203AF8690017FD66 /* SDCategoriesTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 32B99E8A203AF8690017FD66 /* SDCategoriesTests.m */; }; 32B99E9B203B2EDD0017FD66 /* SDTestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D7AF05F1F329763000083C2 /* SDTestCase.m */; }; 32B99E9C203B2EE40017FD66 /* SDCategoriesTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 32B99E8A203AF8690017FD66 /* SDCategoriesTests.m */; }; @@ -208,6 +212,7 @@ 3299222A2365D9A100EAFD97 /* Tests TV.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Tests TV.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; 3299222E2365D9A100EAFD97 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 32A571552037DB2D002EDAAE /* SDAnimatedImageTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDAnimatedImageTest.m; sourceTree = ""; }; + 32B4A47F2C082A160004E42C /* TestImage.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = TestImage.svg; sourceTree = ""; }; 32B99E8A203AF8690017FD66 /* SDCategoriesTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDCategoriesTests.m; sourceTree = ""; }; 32B99E92203B2DF90017FD66 /* Tests Mac.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Tests Mac.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; 32B99E96203B2DF90017FD66 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -351,6 +356,7 @@ 32905E63211D786E00460FCF /* TestImage.heif */, 3234306123E2BAC800C290C8 /* TestImage.pdf */, 32F788A2290D252200B57A1C /* TestImage.nef */, + 32B4A47F2C082A160004E42C /* TestImage.svg */, 327054E1206CEFF3006EA328 /* TestImageAnimated.apng */, 3297A09E23374D1600814590 /* TestImageAnimated.heic */, 32515F9824AF1919005E8F79 /* TestImageAnimated.webp */, @@ -599,6 +605,7 @@ 32464A9E2B7B1833006BE70E /* TestImageAnimated.webp in Resources */, 32464A972B7B1833006BE70E /* TestImage.bmp in Resources */, 32464AA32B7B1833006BE70E /* TestImage.heif in Resources */, + 32B4A4832C082A160004E42C /* TestImage.svg in Resources */, 32464AA12B7B1833006BE70E /* MonochromeTestImage.jpg in Resources */, 32464AA42B7B1833006BE70E /* TestImageLarge.png in Resources */, 32464A962B7B1833006BE70E /* TestImage.gif in Resources */, @@ -628,6 +635,7 @@ 3278F5E42B04C1AC0004A6EE /* IndexedPNG.png in Resources */, 329922842365DC6C00EAFD97 /* MonochromeTestImage.jpg in Resources */, 329922882365DC6C00EAFD97 /* TestImage.jpg in Resources */, + 32B4A4822C082A160004E42C /* TestImage.svg in Resources */, 32515F9E24AF1919005E8F79 /* TestImageAnimated.webp in Resources */, 3299228E2365DC6C00EAFD97 /* TestImageAnimated.heic in Resources */, 32515F9B24AF1919005E8F79 /* TestImageStatic.webp in Resources */, @@ -657,6 +665,7 @@ 3278F5E32B04C1AC0004A6EE /* IndexedPNG.png in Resources */, 32B99EA2203B31360017FD66 /* MonochromeTestImage.jpg in Resources */, 32905E65211D786E00460FCF /* TestImage.heif in Resources */, + 32B4A4812C082A160004E42C /* TestImage.svg in Resources */, 32515F9D24AF1919005E8F79 /* TestImageAnimated.webp in Resources */, 327A418D211D660600495442 /* TestImage.heic in Resources */, 32515F9A24AF1919005E8F79 /* TestImageStatic.webp in Resources */, @@ -686,6 +695,7 @@ 3278F5E22B04C1AC0004A6EE /* IndexedPNG.png in Resources */, 3297A09F23374D1700814590 /* TestImageAnimated.heic in Resources */, 327054E2206CEFF3006EA328 /* TestImageAnimated.apng in Resources */, + 32B4A4802C082A160004E42C /* TestImage.svg in Resources */, 32515F9C24AF1919005E8F79 /* TestImageAnimated.webp in Resources */, 326E69472334C0C300B7252C /* TestLoopCount.gif in Resources */, 32515F9924AF1919005E8F79 /* TestImageStatic.webp in Resources */, diff --git a/Tests/Tests/Images/TestImage.svg b/Tests/Tests/Images/TestImage.svg new file mode 100644 index 00000000..acb906f9 --- /dev/null +++ b/Tests/Tests/Images/TestImage.svg @@ -0,0 +1,4 @@ + + + + diff --git a/Tests/Tests/SDImageCoderTests.m b/Tests/Tests/SDImageCoderTests.m index 61c3a513..1cc0e778 100644 --- a/Tests/Tests/SDImageCoderTests.m +++ b/Tests/Tests/SDImageCoderTests.m @@ -575,6 +575,23 @@ expect(a1).beCloseToWithin(0.33, 0.01); } +- (void)test31ThatSVGShouldUseNativeImageClass { + NSURL *url = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImage" withExtension:@"svg"]; + NSData *data = [NSData dataWithContentsOfURL:url]; + SDAnimatedImage *animatedImage = [SDAnimatedImage imageWithData:data]; + expect(animatedImage).beNil(); + UIImage *image = [UIImage sd_imageWithData:data]; + Class SVGCoderClass = NSClassFromString(@"SDImageSVGCoder"); + if (SVGCoderClass && [SVGCoderClass sharedCoder]) { + expect(image).notTo.beNil(); + // Vector version + expect(image.sd_isVector).beTruthy(); + } else { + // Platform does not support SVG + expect(image).beNil(); + } +} + #pragma mark - Utils - (void)verifyCoder:(id)coder From 99dc9a8e61aad839def448f0440508b14806fe64 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 1 Jul 2024 18:28:20 +0800 Subject: [PATCH 3/3] [NFC] Fix the variable naming --- SDWebImage/Core/SDImageCacheDefine.m | 6 +++--- SDWebImage/Core/SDImageLoader.m | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/SDWebImage/Core/SDImageCacheDefine.m b/SDWebImage/Core/SDImageCacheDefine.m index 741d57cd..a0d1628d 100644 --- a/SDWebImage/Core/SDImageCacheDefine.m +++ b/SDWebImage/Core/SDImageCacheDefine.m @@ -124,9 +124,9 @@ UIImage * _Nullable SDImageCacheDecodeImageData(NSData * _Nonnull imageData, NSS } if (image) { SDImageForceDecodePolicy policy = SDImageForceDecodePolicyAutomatic; - NSNumber *polivyValue = context[SDWebImageContextImageForceDecodePolicy]; - if (polivyValue != nil) { - policy = polivyValue.unsignedIntegerValue; + NSNumber *policyValue = context[SDWebImageContextImageForceDecodePolicy]; + if (policyValue != nil) { + policy = policyValue.unsignedIntegerValue; } // TODO: Deprecated, remove in SD 6.0... #pragma clang diagnostic push diff --git a/SDWebImage/Core/SDImageLoader.m b/SDWebImage/Core/SDImageLoader.m index b302b5b9..9c6c2687 100644 --- a/SDWebImage/Core/SDImageLoader.m +++ b/SDWebImage/Core/SDImageLoader.m @@ -75,9 +75,9 @@ UIImage * _Nullable SDImageLoaderDecodeImageData(NSData * _Nonnull imageData, NS } if (image) { SDImageForceDecodePolicy policy = SDImageForceDecodePolicyAutomatic; - NSNumber *polivyValue = context[SDWebImageContextImageForceDecodePolicy]; - if (polivyValue != nil) { - policy = polivyValue.unsignedIntegerValue; + NSNumber *policyValue = context[SDWebImageContextImageForceDecodePolicy]; + if (policyValue != nil) { + policy = policyValue.unsignedIntegerValue; } // TODO: Deprecated, remove in SD 6.0... #pragma clang diagnostic push @@ -156,9 +156,9 @@ UIImage * _Nullable SDImageLoaderDecodeProgressiveImageData(NSData * _Nonnull im } if (image) { SDImageForceDecodePolicy policy = SDImageForceDecodePolicyAutomatic; - NSNumber *polivyValue = context[SDWebImageContextImageForceDecodePolicy]; - if (polivyValue != nil) { - policy = polivyValue.unsignedIntegerValue; + NSNumber *policyValue = context[SDWebImageContextImageForceDecodePolicy]; + if (policyValue != nil) { + policy = policyValue.unsignedIntegerValue; } // TODO: Deprecated, remove in SD 6.0... #pragma clang diagnostic push