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

This commit is contained in:
DreamPiggy 2020-01-30 13:50:53 +08:00
parent cb84dbb273
commit 8ca4556066
2 changed files with 47 additions and 4 deletions

View File

@ -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.

View File

@ -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