From 8ca455606655a9f57d9e315f91595eb9235107cf Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Thu, 30 Jan 2020 13:50:53 +0800 Subject: [PATCH] Added the `sd_isVector` API on UIImage+Metadata, useful for case when we want to filter the vector/bitmap images. Vector currently only sipports PDF/SVG --- SDWebImage/Core/UIImage+Metadata.h | 10 +++++++- SDWebImage/Core/UIImage+Metadata.m | 41 +++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/SDWebImage/Core/UIImage+Metadata.h b/SDWebImage/Core/UIImage+Metadata.h index e42ff697..8328c261 100644 --- a/SDWebImage/Core/UIImage+Metadata.h +++ b/SDWebImage/Core/UIImage+Metadata.h @@ -28,12 +28,20 @@ /** * UIKit: - * Check the `images` array property + * Check the `images` array property. * AppKit: * NSImage currently only support animated via GIF imageRep unlike UIImage. It will check the imageRep's frame count. */ @property (nonatomic, assign, readonly) BOOL sd_isAnimated; +/** + * UIKit: + * Check the `isSymbolImage` property. Also check the system PDF(iOS 11+) && SVG(iOS 13+) support. + * AppKit: + * NSImage supports PDF && SVG && EPS imageRep, check the imageRep class. + */ +@property (nonatomic, assign, readonly) BOOL sd_isVector; + /** * The image format represent the original compressed image data format. * If you don't manually specify a format, this information is retrieve from CGImage using `CGImageGetUTType`, which may return nil for non-CG based image. At this time it will return `SDImageFormatUndefined` as default value. diff --git a/SDWebImage/Core/UIImage+Metadata.m b/SDWebImage/Core/UIImage+Metadata.m index 3c9bf929..ef63c41f 100644 --- a/SDWebImage/Core/UIImage+Metadata.m +++ b/SDWebImage/Core/UIImage+Metadata.m @@ -32,6 +32,26 @@ return (self.images != nil); } +- (BOOL)sd_isVector { + if (@available(iOS 13.0, tvOS 13.0, watchOS 6.0, *)) { + if (self.isSymbolImage) { + return YES; + } + } +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Warc-performSelector-leaks" + SEL SVGSelector = NSSelectorFromString(@"_CGSVGDocument"); + if ([self respondsToSelector:SVGSelector] && [self performSelector:SVGSelector] != nil) { + return YES; + } + SEL PDFSelector = NSSelectorFromString(@"_CGPDFPage"); + if ([self respondsToSelector:PDFSelector] && [self performSelector:PDFSelector] != nil) { + return YES; + } +#pragma clang diagnostic pop + return NO; +} + #else - (NSUInteger)sd_imageLoopCount { @@ -61,7 +81,7 @@ } - (BOOL)sd_isAnimated { - BOOL isGIF = NO; + BOOL isAnimated = NO; NSRect imageRect = NSMakeRect(0, 0, self.size.width, self.size.height); NSImageRep *imageRep = [self bestRepresentationForRect:imageRect context:nil hints:nil]; NSBitmapImageRep *bitmapImageRep; @@ -70,9 +90,24 @@ } if (bitmapImageRep) { NSUInteger frameCount = [[bitmapImageRep valueForProperty:NSImageFrameCount] unsignedIntegerValue]; - isGIF = frameCount > 1 ? YES : NO; + isAnimated = frameCount > 1 ? YES : NO; } - return isGIF; + return isAnimated; +} + +- (BOOL)sd_isVector { + NSRect imageRect = NSMakeRect(0, 0, self.size.width, self.size.height); + NSImageRep *imageRep = [self bestRepresentationForRect:imageRect context:nil hints:nil]; + if ([imageRep isKindOfClass:[NSPDFImageRep class]]) { + return YES; + } + if ([imageRep isKindOfClass:[NSEPSImageRep class]]) { + return YES; + } + if ([NSStringFromClass(imageRep.class) hasSuffix:@"NSSVGImageRep"]) { + return YES; + } + return NO; } #endif