Merge pull request #3724 from dreampiggy/bugfix/vector_sdanimatedImage

Fix the issue that SDAnimatedImage breaks the vector on macOS
This commit is contained in:
DreamPiggy 2024-07-01 19:01:14 +08:00 committed by GitHub
commit 6b629e1727
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 51 additions and 10 deletions

View File

@ -72,7 +72,7 @@
// This class override these methods from UIImage(NSImage), and it supports NSSecureCoding. // 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. // 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. // @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 + (nullable instancetype)imageNamed:(nonnull NSString *)name; // Cache in memory, no Asset Catalog support
#if __has_include(<UIKit/UITraitCollection.h>) #if __has_include(<UIKit/UITraitCollection.h>)

View File

@ -141,6 +141,12 @@ static CGFloat SDImageScaleFromPath(NSString *string) {
if (!data || data.length == 0) { if (!data || data.length == 0) {
return nil; return nil;
} }
// Vector image does not supported, guard firstly
SDImageFormat format = [NSData sd_imageFormatForImageData:data];
if (format == SDImageFormatSVG || format == SDImageFormatPDF) {
return nil;
}
id<SDAnimatedImageCoder> animatedCoder = nil; id<SDAnimatedImageCoder> animatedCoder = nil;
SDImageCoderMutableOptions *mutableCoderOptions; SDImageCoderMutableOptions *mutableCoderOptions;
if (options != nil) { if (options != nil) {
@ -167,6 +173,10 @@ static CGFloat SDImageScaleFromPath(NSString *string) {
if (!image) { if (!image) {
return nil; return nil;
} }
// Vector image does not supported, guard secondly
if (image.sd_isVector) {
return nil;
}
#if SD_MAC #if SD_MAC
self = [super initWithCGImage:image.CGImage scale:MAX(scale, 1) orientation:kCGImagePropertyOrientationUp]; self = [super initWithCGImage:image.CGImage scale:MAX(scale, 1) orientation:kCGImagePropertyOrientationUp];
#else #else

View File

@ -124,9 +124,9 @@ UIImage * _Nullable SDImageCacheDecodeImageData(NSData * _Nonnull imageData, NSS
} }
if (image) { if (image) {
SDImageForceDecodePolicy policy = SDImageForceDecodePolicyAutomatic; SDImageForceDecodePolicy policy = SDImageForceDecodePolicyAutomatic;
NSNumber *polivyValue = context[SDWebImageContextImageForceDecodePolicy]; NSNumber *policyValue = context[SDWebImageContextImageForceDecodePolicy];
if (polivyValue != nil) { if (policyValue != nil) {
policy = polivyValue.unsignedIntegerValue; policy = policyValue.unsignedIntegerValue;
} }
// TODO: Deprecated, remove in SD 6.0... // TODO: Deprecated, remove in SD 6.0...
#pragma clang diagnostic push #pragma clang diagnostic push

View File

@ -75,9 +75,9 @@ UIImage * _Nullable SDImageLoaderDecodeImageData(NSData * _Nonnull imageData, NS
} }
if (image) { if (image) {
SDImageForceDecodePolicy policy = SDImageForceDecodePolicyAutomatic; SDImageForceDecodePolicy policy = SDImageForceDecodePolicyAutomatic;
NSNumber *polivyValue = context[SDWebImageContextImageForceDecodePolicy]; NSNumber *policyValue = context[SDWebImageContextImageForceDecodePolicy];
if (polivyValue != nil) { if (policyValue != nil) {
policy = polivyValue.unsignedIntegerValue; policy = policyValue.unsignedIntegerValue;
} }
// TODO: Deprecated, remove in SD 6.0... // TODO: Deprecated, remove in SD 6.0...
#pragma clang diagnostic push #pragma clang diagnostic push
@ -156,9 +156,9 @@ UIImage * _Nullable SDImageLoaderDecodeProgressiveImageData(NSData * _Nonnull im
} }
if (image) { if (image) {
SDImageForceDecodePolicy policy = SDImageForceDecodePolicyAutomatic; SDImageForceDecodePolicy policy = SDImageForceDecodePolicyAutomatic;
NSNumber *polivyValue = context[SDWebImageContextImageForceDecodePolicy]; NSNumber *policyValue = context[SDWebImageContextImageForceDecodePolicy];
if (polivyValue != nil) { if (policyValue != nil) {
policy = polivyValue.unsignedIntegerValue; policy = policyValue.unsignedIntegerValue;
} }
// TODO: Deprecated, remove in SD 6.0... // TODO: Deprecated, remove in SD 6.0...
#pragma clang diagnostic push #pragma clang diagnostic push

View File

@ -125,6 +125,10 @@
3299228D2365DC6C00EAFD97 /* TestImageAnimated.apng in Resources */ = {isa = PBXBuildFile; fileRef = 327054E1206CEFF3006EA328 /* TestImageAnimated.apng */; }; 3299228D2365DC6C00EAFD97 /* TestImageAnimated.apng in Resources */ = {isa = PBXBuildFile; fileRef = 327054E1206CEFF3006EA328 /* TestImageAnimated.apng */; };
3299228E2365DC6C00EAFD97 /* TestImageAnimated.heic in Resources */ = {isa = PBXBuildFile; fileRef = 3297A09E23374D1600814590 /* TestImageAnimated.heic */; }; 3299228E2365DC6C00EAFD97 /* TestImageAnimated.heic in Resources */ = {isa = PBXBuildFile; fileRef = 3297A09E23374D1600814590 /* TestImageAnimated.heic */; };
32A571562037DB2D002EDAAE /* SDAnimatedImageTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 32A571552037DB2D002EDAAE /* SDAnimatedImageTest.m */; }; 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 */; }; 32B99E8B203AF8690017FD66 /* SDCategoriesTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 32B99E8A203AF8690017FD66 /* SDCategoriesTests.m */; };
32B99E9B203B2EDD0017FD66 /* SDTestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D7AF05F1F329763000083C2 /* SDTestCase.m */; }; 32B99E9B203B2EDD0017FD66 /* SDTestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D7AF05F1F329763000083C2 /* SDTestCase.m */; };
32B99E9C203B2EE40017FD66 /* SDCategoriesTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 32B99E8A203AF8690017FD66 /* SDCategoriesTests.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; }; 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 = "<group>"; }; 3299222E2365D9A100EAFD97 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
32A571552037DB2D002EDAAE /* SDAnimatedImageTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDAnimatedImageTest.m; sourceTree = "<group>"; }; 32A571552037DB2D002EDAAE /* SDAnimatedImageTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDAnimatedImageTest.m; sourceTree = "<group>"; };
32B4A47F2C082A160004E42C /* TestImage.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = TestImage.svg; sourceTree = "<group>"; };
32B99E8A203AF8690017FD66 /* SDCategoriesTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDCategoriesTests.m; sourceTree = "<group>"; }; 32B99E8A203AF8690017FD66 /* SDCategoriesTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDCategoriesTests.m; sourceTree = "<group>"; };
32B99E92203B2DF90017FD66 /* Tests Mac.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Tests Mac.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; 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 = "<group>"; }; 32B99E96203B2DF90017FD66 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@ -351,6 +356,7 @@
32905E63211D786E00460FCF /* TestImage.heif */, 32905E63211D786E00460FCF /* TestImage.heif */,
3234306123E2BAC800C290C8 /* TestImage.pdf */, 3234306123E2BAC800C290C8 /* TestImage.pdf */,
32F788A2290D252200B57A1C /* TestImage.nef */, 32F788A2290D252200B57A1C /* TestImage.nef */,
32B4A47F2C082A160004E42C /* TestImage.svg */,
327054E1206CEFF3006EA328 /* TestImageAnimated.apng */, 327054E1206CEFF3006EA328 /* TestImageAnimated.apng */,
3297A09E23374D1600814590 /* TestImageAnimated.heic */, 3297A09E23374D1600814590 /* TestImageAnimated.heic */,
32515F9824AF1919005E8F79 /* TestImageAnimated.webp */, 32515F9824AF1919005E8F79 /* TestImageAnimated.webp */,
@ -599,6 +605,7 @@
32464A9E2B7B1833006BE70E /* TestImageAnimated.webp in Resources */, 32464A9E2B7B1833006BE70E /* TestImageAnimated.webp in Resources */,
32464A972B7B1833006BE70E /* TestImage.bmp in Resources */, 32464A972B7B1833006BE70E /* TestImage.bmp in Resources */,
32464AA32B7B1833006BE70E /* TestImage.heif in Resources */, 32464AA32B7B1833006BE70E /* TestImage.heif in Resources */,
32B4A4832C082A160004E42C /* TestImage.svg in Resources */,
32464AA12B7B1833006BE70E /* MonochromeTestImage.jpg in Resources */, 32464AA12B7B1833006BE70E /* MonochromeTestImage.jpg in Resources */,
32464AA42B7B1833006BE70E /* TestImageLarge.png in Resources */, 32464AA42B7B1833006BE70E /* TestImageLarge.png in Resources */,
32464A962B7B1833006BE70E /* TestImage.gif in Resources */, 32464A962B7B1833006BE70E /* TestImage.gif in Resources */,
@ -628,6 +635,7 @@
3278F5E42B04C1AC0004A6EE /* IndexedPNG.png in Resources */, 3278F5E42B04C1AC0004A6EE /* IndexedPNG.png in Resources */,
329922842365DC6C00EAFD97 /* MonochromeTestImage.jpg in Resources */, 329922842365DC6C00EAFD97 /* MonochromeTestImage.jpg in Resources */,
329922882365DC6C00EAFD97 /* TestImage.jpg in Resources */, 329922882365DC6C00EAFD97 /* TestImage.jpg in Resources */,
32B4A4822C082A160004E42C /* TestImage.svg in Resources */,
32515F9E24AF1919005E8F79 /* TestImageAnimated.webp in Resources */, 32515F9E24AF1919005E8F79 /* TestImageAnimated.webp in Resources */,
3299228E2365DC6C00EAFD97 /* TestImageAnimated.heic in Resources */, 3299228E2365DC6C00EAFD97 /* TestImageAnimated.heic in Resources */,
32515F9B24AF1919005E8F79 /* TestImageStatic.webp in Resources */, 32515F9B24AF1919005E8F79 /* TestImageStatic.webp in Resources */,
@ -657,6 +665,7 @@
3278F5E32B04C1AC0004A6EE /* IndexedPNG.png in Resources */, 3278F5E32B04C1AC0004A6EE /* IndexedPNG.png in Resources */,
32B99EA2203B31360017FD66 /* MonochromeTestImage.jpg in Resources */, 32B99EA2203B31360017FD66 /* MonochromeTestImage.jpg in Resources */,
32905E65211D786E00460FCF /* TestImage.heif in Resources */, 32905E65211D786E00460FCF /* TestImage.heif in Resources */,
32B4A4812C082A160004E42C /* TestImage.svg in Resources */,
32515F9D24AF1919005E8F79 /* TestImageAnimated.webp in Resources */, 32515F9D24AF1919005E8F79 /* TestImageAnimated.webp in Resources */,
327A418D211D660600495442 /* TestImage.heic in Resources */, 327A418D211D660600495442 /* TestImage.heic in Resources */,
32515F9A24AF1919005E8F79 /* TestImageStatic.webp in Resources */, 32515F9A24AF1919005E8F79 /* TestImageStatic.webp in Resources */,
@ -686,6 +695,7 @@
3278F5E22B04C1AC0004A6EE /* IndexedPNG.png in Resources */, 3278F5E22B04C1AC0004A6EE /* IndexedPNG.png in Resources */,
3297A09F23374D1700814590 /* TestImageAnimated.heic in Resources */, 3297A09F23374D1700814590 /* TestImageAnimated.heic in Resources */,
327054E2206CEFF3006EA328 /* TestImageAnimated.apng in Resources */, 327054E2206CEFF3006EA328 /* TestImageAnimated.apng in Resources */,
32B4A4802C082A160004E42C /* TestImage.svg in Resources */,
32515F9C24AF1919005E8F79 /* TestImageAnimated.webp in Resources */, 32515F9C24AF1919005E8F79 /* TestImageAnimated.webp in Resources */,
326E69472334C0C300B7252C /* TestLoopCount.gif in Resources */, 326E69472334C0C300B7252C /* TestLoopCount.gif in Resources */,
32515F9924AF1919005E8F79 /* TestImageStatic.webp in Resources */, 32515F9924AF1919005E8F79 /* TestImageStatic.webp in Resources */,

View File

@ -0,0 +1,4 @@
<svg xmlns='http://www.w3.org/2000/svg' viewBox="0 0 131 76">
<path d="M36,5l12,41l12-41h33v4l-13,21c30,10,2,69-21,28l7-2c15,27,33,-22,3,-19v-4l12-20h-15l-17,59h-1l-13-42l-12,42h-1l-20-67h9l12,41l8-28l-4-13h9" fill='#005A9C'/>
<path d="M94,53c15,32,30,14,35,7l-1-7c-16,26-32,3-34,0M122,16c-10-21-34,0-21,30c-5-30 16,-38 23,-21l5-10l-2-9"/>
</svg>

After

Width:  |  Height:  |  Size: 351 B

View File

@ -575,6 +575,23 @@
expect(a1).beCloseToWithin(0.33, 0.01); 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 #pragma mark - Utils
- (void)verifyCoder:(id<SDImageCoder>)coder - (void)verifyCoder:(id<SDImageCoder>)coder