From 6fb20ce312031c1c576025e1abaee882a47d4143 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 6 Jul 2020 11:22:53 +0800 Subject: [PATCH] Refactory the implementation of that canDecode/canEncode, use single method with Set to check, instead of creating different internal files --- SDWebImage.xcodeproj/project.pbxproj | 8 ---- SDWebImage/Core/NSData+ImageContentType.m | 3 +- SDWebImage/Core/SDAnimatedImageRep.m | 6 ++- SDWebImage/Core/SDImageAWebPCoder.m | 25 +--------- SDWebImage/Core/SDImageHEICCoder.m | 47 ++----------------- SDWebImage/Core/SDImageIOAnimatedCoder.m | 26 +++++----- .../Private/SDImageAWebPCoderInternal.h | 20 -------- SDWebImage/Private/SDImageHEICCoderInternal.h | 25 ---------- .../Private/SDImageIOAnimatedCoderInternal.h | 8 ++++ 9 files changed, 34 insertions(+), 134 deletions(-) delete mode 100644 SDWebImage/Private/SDImageAWebPCoderInternal.h delete mode 100644 SDWebImage/Private/SDImageHEICCoderInternal.h diff --git a/SDWebImage.xcodeproj/project.pbxproj b/SDWebImage.xcodeproj/project.pbxproj index b21226d8..87e5bae1 100644 --- a/SDWebImage.xcodeproj/project.pbxproj +++ b/SDWebImage.xcodeproj/project.pbxproj @@ -103,7 +103,6 @@ 325F7CCB238942AB00AEDFCC /* UIImage+ExtendedCacheData.m in Sources */ = {isa = PBXBuildFile; fileRef = 325F7CC9238942AB00AEDFCC /* UIImage+ExtendedCacheData.m */; }; 325F7CCC2389463D00AEDFCC /* UIImage+ExtendedCacheData.m in Sources */ = {isa = PBXBuildFile; fileRef = 325F7CC9238942AB00AEDFCC /* UIImage+ExtendedCacheData.m */; }; 325F7CCD2389467800AEDFCC /* UIImage+ExtendedCacheData.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 325F7CC8238942AB00AEDFCC /* UIImage+ExtendedCacheData.h */; }; - 3263626B24AEEE94008FB119 /* SDImageAWebPCoderInternal.h in Headers */ = {isa = PBXBuildFile; fileRef = 3263626A24AEEE94008FB119 /* SDImageAWebPCoderInternal.h */; settings = {ATTRIBUTES = (Private, ); }; }; 3263626E24AEEEB0008FB119 /* SDImageAWebPCoder.h in Headers */ = {isa = PBXBuildFile; fileRef = 3263626C24AEEEB0008FB119 /* SDImageAWebPCoder.h */; settings = {ATTRIBUTES = (Public, ); }; }; 3263626F24AEEEB0008FB119 /* SDImageAWebPCoder.m in Sources */ = {isa = PBXBuildFile; fileRef = 3263626D24AEEEB0008FB119 /* SDImageAWebPCoder.m */; }; 326E2F2E236F0B23006F847F /* SDAnimatedImagePlayer.h in Headers */ = {isa = PBXBuildFile; fileRef = 326E2F2C236F0B23006F847F /* SDAnimatedImagePlayer.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -190,7 +189,6 @@ 3298655D2337230C0071958B /* SDImageHEICCoder.m in Sources */ = {isa = PBXBuildFile; fileRef = 3298655B2337230C0071958B /* SDImageHEICCoder.m */; }; 3298655E2337230C0071958B /* SDImageHEICCoder.m in Sources */ = {isa = PBXBuildFile; fileRef = 3298655B2337230C0071958B /* SDImageHEICCoder.m */; }; 3298655F233723220071958B /* SDImageHEICCoder.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3298655A2337230C0071958B /* SDImageHEICCoder.h */; }; - 32986562233737C70071958B /* SDImageHEICCoderInternal.h in Headers */ = {isa = PBXBuildFile; fileRef = 32986560233737C70071958B /* SDImageHEICCoderInternal.h */; settings = {ATTRIBUTES = (Private, ); }; }; 329A185B1FFF5DFD008C9A2F /* UIImage+Metadata.h in Headers */ = {isa = PBXBuildFile; fileRef = 329A18571FFF5DFD008C9A2F /* UIImage+Metadata.h */; settings = {ATTRIBUTES = (Public, ); }; }; 329A185F1FFF5DFD008C9A2F /* UIImage+Metadata.m in Sources */ = {isa = PBXBuildFile; fileRef = 329A18581FFF5DFD008C9A2F /* UIImage+Metadata.m */; }; 329A18611FFF5DFD008C9A2F /* UIImage+Metadata.m in Sources */ = {isa = PBXBuildFile; fileRef = 329A18581FFF5DFD008C9A2F /* UIImage+Metadata.m */; }; @@ -440,7 +438,6 @@ 325F7CC523893B2E00AEDFCC /* SDFileAttributeHelper.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDFileAttributeHelper.m; sourceTree = ""; }; 325F7CC8238942AB00AEDFCC /* UIImage+ExtendedCacheData.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "UIImage+ExtendedCacheData.h"; path = "Core/UIImage+ExtendedCacheData.h"; sourceTree = ""; }; 325F7CC9238942AB00AEDFCC /* UIImage+ExtendedCacheData.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = "UIImage+ExtendedCacheData.m"; path = "Core/UIImage+ExtendedCacheData.m"; sourceTree = ""; }; - 3263626A24AEEE94008FB119 /* SDImageAWebPCoderInternal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDImageAWebPCoderInternal.h; sourceTree = ""; }; 3263626C24AEEEB0008FB119 /* SDImageAWebPCoder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDImageAWebPCoder.h; path = Core/SDImageAWebPCoder.h; sourceTree = ""; }; 3263626D24AEEEB0008FB119 /* SDImageAWebPCoder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SDImageAWebPCoder.m; path = Core/SDImageAWebPCoder.m; sourceTree = ""; }; 326E2F2C236F0B23006F847F /* SDAnimatedImagePlayer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SDAnimatedImagePlayer.h; path = Core/SDAnimatedImagePlayer.h; sourceTree = ""; }; @@ -464,7 +461,6 @@ 3290FA031FA478AF0047D20C /* SDImageFrame.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = SDImageFrame.m; path = Core/SDImageFrame.m; sourceTree = ""; }; 3298655A2337230C0071958B /* SDImageHEICCoder.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SDImageHEICCoder.h; path = Core/SDImageHEICCoder.h; sourceTree = ""; }; 3298655B2337230C0071958B /* SDImageHEICCoder.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = SDImageHEICCoder.m; path = Core/SDImageHEICCoder.m; sourceTree = ""; }; - 32986560233737C70071958B /* SDImageHEICCoderInternal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SDImageHEICCoderInternal.h; sourceTree = ""; }; 329A18571FFF5DFD008C9A2F /* UIImage+Metadata.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIImage+Metadata.h"; path = "Core/UIImage+Metadata.h"; sourceTree = ""; }; 329A18581FFF5DFD008C9A2F /* UIImage+Metadata.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIImage+Metadata.m"; path = "Core/UIImage+Metadata.m"; sourceTree = ""; }; 329F1235223FAA3B00B309FD /* SDmetamacros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDmetamacros.h; sourceTree = ""; }; @@ -690,8 +686,6 @@ 325C460C223394D8004CAE11 /* SDImageCachesManagerOperation.h */, 325C460D223394D8004CAE11 /* SDImageCachesManagerOperation.m */, 32C78E39233371AD00C6B7F8 /* SDImageIOAnimatedCoderInternal.h */, - 32986560233737C70071958B /* SDImageHEICCoderInternal.h */, - 3263626A24AEEE94008FB119 /* SDImageAWebPCoderInternal.h */, 3253F235244982D3006C2BE8 /* SDWebImageTransitionInternal.h */, 325C461E2233A02E004CAE11 /* UIColor+SDHexString.h */, 325C461F2233A02E004CAE11 /* UIColor+SDHexString.m */, @@ -900,7 +894,6 @@ buildActionMask = 2147483647; files = ( 32B5CC60222F89C2005EB74E /* SDAsyncBlockOperation.h in Headers */, - 32986562233737C70071958B /* SDImageHEICCoderInternal.h in Headers */, 32D122202080B2EB003685A3 /* SDImageCacheDefine.h in Headers */, 3298655C2337230C0071958B /* SDImageHEICCoder.h in Headers */, 32B9B539206ED4230026769D /* SDWebImageDownloaderConfig.h in Headers */, @@ -923,7 +916,6 @@ 43A918661D8308FE00B3925F /* SDImageCacheConfig.h in Headers */, 3290FA061FA478AF0047D20C /* SDImageFrame.h in Headers */, 326E2F33236F1D58006F847F /* SDDeviceHelper.h in Headers */, - 3263626B24AEEE94008FB119 /* SDImageAWebPCoderInternal.h in Headers */, 329F1237223FAA3B00B309FD /* SDmetamacros.h in Headers */, 324DF4B6200A14DC008A84CC /* SDWebImageDefine.h in Headers */, 32A09E3F233358B700339F9D /* SDImageIOAnimatedCoder.h in Headers */, diff --git a/SDWebImage/Core/NSData+ImageContentType.m b/SDWebImage/Core/NSData+ImageContentType.m index 191a4348..74eae467 100644 --- a/SDWebImage/Core/NSData+ImageContentType.m +++ b/SDWebImage/Core/NSData+ImageContentType.m @@ -13,8 +13,7 @@ #else #import #endif -#import "SDImageHEICCoderInternal.h" -#import "SDImageAWebPCoderInternal.h" +#import "SDImageIOAnimatedCoderInternal.h" #define kSVGTagEnd @"" diff --git a/SDWebImage/Core/SDAnimatedImageRep.m b/SDWebImage/Core/SDAnimatedImageRep.m index ffbb4fe6..08317429 100644 --- a/SDWebImage/Core/SDAnimatedImageRep.m +++ b/SDWebImage/Core/SDAnimatedImageRep.m @@ -14,8 +14,7 @@ #import "SDImageGIFCoder.h" #import "SDImageAPNGCoder.h" #import "SDImageHEICCoder.h" -#import "SDImageHEICCoderInternal.h" -#import "SDImageAWebPCoderInternal.h" +#import "SDImageAWebPCoder.h" @implementation SDAnimatedImageRep { CGImageSourceRef _imageSource; @@ -35,6 +34,8 @@ } // We should override init method for `NSBitmapImageRep` to do initialize about animated image format +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunguarded-availability" - (instancetype)initWithData:(NSData *)data { self = [super initWithData:data]; if (self) { @@ -119,6 +120,7 @@ [super setProperty:NSImageCurrentFrameDuration withValue:@(frameDuration)]; } } +#pragma clang diagnostic pop @end diff --git a/SDWebImage/Core/SDImageAWebPCoder.m b/SDWebImage/Core/SDImageAWebPCoder.m index 6b770a80..e58bc215 100644 --- a/SDWebImage/Core/SDImageAWebPCoder.m +++ b/SDWebImage/Core/SDImageAWebPCoder.m @@ -7,7 +7,6 @@ */ #import "SDImageAWebPCoder.h" -#import "SDImageAWebPCoderInternal.h" #import "SDImageIOAnimatedCoderInternal.h" // These constants are available from iOS 14+ and Xcode 12. This raw value is used for toolchain and firmware compatibility @@ -46,7 +45,7 @@ static NSString * kSDCGImagePropertyWebPUnclampedDelayTime = @"UnclampedDelayTim switch ([NSData sd_imageFormatForImageData:data]) { case SDImageFormatWebP: // Check WebP decoding compatibility - return [self.class canDecodeFromWebPFormat]; + return [self.class canDecodeFromFormat:SDImageFormatWebP]; default: return NO; } @@ -60,32 +59,12 @@ static NSString * kSDCGImagePropertyWebPUnclampedDelayTime = @"UnclampedDelayTim switch (format) { case SDImageFormatWebP: // Check WebP encoding compatibility - return [self.class canEncodeToWebPFormat]; + return [self.class canEncodeToFormat:SDImageFormatWebP]; default: return NO; } } -#pragma mark - WebP Format - -+ (BOOL)canDecodeFromWebPFormat { - static BOOL canDecode = NO; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - canDecode = [self canDecodeFromFormat:SDImageFormatWebP]; - }); - return canDecode; -} - -+ (BOOL)canEncodeToWebPFormat { - static BOOL canEncode = NO; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - canEncode = [self canEncodeToFormat:SDImageFormatWebP]; - }); - return canEncode; -} - #pragma mark - Subclass Override + (SDImageFormat)imageFormat { diff --git a/SDWebImage/Core/SDImageHEICCoder.m b/SDWebImage/Core/SDImageHEICCoder.m index 6ed0c751..4f6802a6 100644 --- a/SDWebImage/Core/SDImageHEICCoder.m +++ b/SDWebImage/Core/SDImageHEICCoder.m @@ -7,7 +7,6 @@ */ #import "SDImageHEICCoder.h" -#import "SDImageHEICCoderInternal.h" #import "SDImageIOAnimatedCoderInternal.h" // These constants are available from iOS 13+ and Xcode 11. This raw value is used for toolchain and firmware compatibility @@ -46,10 +45,10 @@ static NSString * kSDCGImagePropertyHEICSUnclampedDelayTime = @"UnclampedDelayTi switch ([NSData sd_imageFormatForImageData:data]) { case SDImageFormatHEIC: // Check HEIC decoding compatibility - return [self.class canDecodeFromHEICFormat]; + return [self.class canDecodeFromFormat:SDImageFormatHEIC]; case SDImageFormatHEIF: // Check HEIF decoding compatibility - return [self.class canDecodeFromHEIFFormat]; + return [self.class canDecodeFromFormat:SDImageFormatHEIF]; default: return NO; } @@ -63,53 +62,15 @@ static NSString * kSDCGImagePropertyHEICSUnclampedDelayTime = @"UnclampedDelayTi switch (format) { case SDImageFormatHEIC: // Check HEIC encoding compatibility - return [self.class canEncodeToHEICFormat]; + return [self.class canEncodeToFormat:SDImageFormatHEIC]; case SDImageFormatHEIF: // Check HEIF encoding compatibility - return [self.class canEncodeToHEIFFormat]; + return [self.class canEncodeToFormat:SDImageFormatHEIF]; default: return NO; } } -#pragma mark - HEIF Format - -+ (BOOL)canDecodeFromHEICFormat { - static BOOL canDecode = NO; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - canDecode = [self canDecodeFromFormat:SDImageFormatHEIC]; - }); - return canDecode; -} - -+ (BOOL)canDecodeFromHEIFFormat { - static BOOL canDecode = NO; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - canDecode = [self canDecodeFromFormat:SDImageFormatHEIF]; - }); - return canDecode; -} - -+ (BOOL)canEncodeToHEICFormat { - static BOOL canEncode = NO; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - canEncode = [self canEncodeToFormat:SDImageFormatHEIC]; - }); - return canEncode; -} - -+ (BOOL)canEncodeToHEIFFormat { - static BOOL canEncode = NO; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - canEncode = [self canEncodeToFormat:SDImageFormatHEIF]; - }); - return canEncode; -} - #pragma mark - Subclass Override + (SDImageFormat)imageFormat { diff --git a/SDWebImage/Core/SDImageIOAnimatedCoder.m b/SDWebImage/Core/SDImageIOAnimatedCoder.m index 514d3238..3d54602c 100644 --- a/SDWebImage/Core/SDImageIOAnimatedCoder.m +++ b/SDWebImage/Core/SDImageIOAnimatedCoder.m @@ -109,9 +109,14 @@ static NSString * kSDCGImageDestinationRequestedFileSize = @"kCGImageDestination #pragma mark - Utils + (BOOL)canDecodeFromFormat:(SDImageFormat)format { + static dispatch_once_t onceToken; + static NSSet *imageUTTypeSet; + dispatch_once(&onceToken, ^{ + NSArray *imageUTTypes = (__bridge_transfer NSArray *)CGImageSourceCopyTypeIdentifiers(); + imageUTTypeSet = [NSSet setWithArray:imageUTTypes]; + }); CFStringRef imageUTType = [NSData sd_UTTypeFromImageFormat:format]; - NSArray *imageUTTypes = (__bridge_transfer NSArray *)CGImageSourceCopyTypeIdentifiers(); - if ([imageUTTypes containsObject:(__bridge NSString *)(imageUTType)]) { + if ([imageUTTypeSet containsObject:(__bridge NSString *)(imageUTType)]) { // Can decode from target format return YES; } @@ -119,19 +124,18 @@ static NSString * kSDCGImageDestinationRequestedFileSize = @"kCGImageDestination } + (BOOL)canEncodeToFormat:(SDImageFormat)format { - NSMutableData *imageData = [NSMutableData data]; + static dispatch_once_t onceToken; + static NSSet *imageUTTypeSet; + dispatch_once(&onceToken, ^{ + NSArray *imageUTTypes = (__bridge_transfer NSArray *)CGImageDestinationCopyTypeIdentifiers(); + imageUTTypeSet = [NSSet setWithArray:imageUTTypes]; + }); CFStringRef imageUTType = [NSData sd_UTTypeFromImageFormat:format]; - - // Create an image destination. - CGImageDestinationRef imageDestination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)imageData, imageUTType, 1, NULL); - if (!imageDestination) { - // Can't encode to target format - return NO; - } else { + if ([imageUTTypeSet containsObject:(__bridge NSString *)(imageUTType)]) { // Can encode to target format - CFRelease(imageDestination); return YES; } + return NO; } + (NSUInteger)imageLoopCountWithSource:(CGImageSourceRef)source { diff --git a/SDWebImage/Private/SDImageAWebPCoderInternal.h b/SDWebImage/Private/SDImageAWebPCoderInternal.h deleted file mode 100644 index f6866107..00000000 --- a/SDWebImage/Private/SDImageAWebPCoderInternal.h +++ /dev/null @@ -1,20 +0,0 @@ -/* -* This file is part of the SDWebImage package. -* (c) Olivier Poitrey -* -* For the full copyright and license information, please view the LICENSE -* file that was distributed with this source code. -*/ - -#import -#import "SDImageAWebPCoder.h" - -// kUTTypeWebP seems not defined in public UTI framework, Apple use the hardcode string, we define them :) -#define kSDUTTypeWebP ((__bridge CFStringRef)@"org.webmproject.webp") - -@interface SDImageAWebPCoder () - -+ (BOOL)canDecodeFromWebPFormat; -+ (BOOL)canEncodeToWebPFormat; - -@end diff --git a/SDWebImage/Private/SDImageHEICCoderInternal.h b/SDWebImage/Private/SDImageHEICCoderInternal.h deleted file mode 100644 index e7017bba..00000000 --- a/SDWebImage/Private/SDImageHEICCoderInternal.h +++ /dev/null @@ -1,25 +0,0 @@ -/* -* This file is part of the SDWebImage package. -* (c) Olivier Poitrey -* -* For the full copyright and license information, please view the LICENSE -* file that was distributed with this source code. -*/ - -#import -#import "SDImageHEICCoder.h" - -// AVFileTypeHEIC/AVFileTypeHEIF is defined in AVFoundation via iOS 11, we use this without import AVFoundation -#define kSDUTTypeHEIC ((__bridge CFStringRef)@"public.heic") -#define kSDUTTypeHEIF ((__bridge CFStringRef)@"public.heif") -// HEIC Sequence (Animated Image) -#define kSDUTTypeHEICS ((__bridge CFStringRef)@"public.heics") - -@interface SDImageHEICCoder () - -+ (BOOL)canDecodeFromHEICFormat; -+ (BOOL)canDecodeFromHEIFFormat; -+ (BOOL)canEncodeToHEICFormat; -+ (BOOL)canEncodeToHEIFFormat; - -@end diff --git a/SDWebImage/Private/SDImageIOAnimatedCoderInternal.h b/SDWebImage/Private/SDImageIOAnimatedCoderInternal.h index f16d77ab..022cf7dc 100644 --- a/SDWebImage/Private/SDImageIOAnimatedCoderInternal.h +++ b/SDWebImage/Private/SDImageIOAnimatedCoderInternal.h @@ -9,6 +9,14 @@ #import #import "SDImageIOAnimatedCoder.h" +// AVFileTypeHEIC/AVFileTypeHEIF is defined in AVFoundation via iOS 11, we use this without import AVFoundation +#define kSDUTTypeHEIC ((__bridge CFStringRef)@"public.heic") +#define kSDUTTypeHEIF ((__bridge CFStringRef)@"public.heif") +// HEIC Sequence (Animated Image) +#define kSDUTTypeHEICS ((__bridge CFStringRef)@"public.heics") +// kUTTypeWebP seems not defined in public UTI framework, Apple use the hardcode string, we define them :) +#define kSDUTTypeWebP ((__bridge CFStringRef)@"org.webmproject.webp") + @interface SDImageIOAnimatedCoder () + (NSTimeInterval)frameDurationAtIndex:(NSUInteger)index source:(nonnull CGImageSourceRef)source;