From 2163691d1130e2b3295c2a58138078294549674b Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 30 Dec 2019 17:41:08 +0800 Subject: [PATCH] Fix the compile issue on watchOS --- SDWebImage/Core/UIImage+Transform.m | 51 +++++++++++++++++------------ 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/SDWebImage/Core/UIImage+Transform.m b/SDWebImage/Core/UIImage+Transform.m index a55eb130..7ff7e98a 100644 --- a/SDWebImage/Core/UIImage+Transform.m +++ b/SDWebImage/Core/UIImage+Transform.m @@ -164,7 +164,9 @@ static inline UIColor * SDGetColorFromPixel(Pixel_8888 pixel, CGBitmapInfo bitma return [UIColor colorWithRed:r green:g blue:b alpha:a]; } -static inline CIColor *SDCIColorConvertFromUIColor(UIColor * _Nonnull color) { +#if SD_UIKIT || SD_MAC +// Core Image Support +static inline CIColor *SDCIColorFromUIColor(UIColor * _Nonnull color) { CGFloat red, green, blue, alpha; #if SD_UIKIT if (![color getRed:&red green:&green blue:&blue alpha:&alpha]) { @@ -186,6 +188,19 @@ static inline CIColor *SDCIColorConvertFromUIColor(UIColor * _Nonnull color) { return ciColor; } +static inline CGImageRef _Nullable SDCGImageFromCIImage(CIImage * _Nonnull ciImage) { + CGImageRef imageRef = NULL; + if (@available(iOS 10, macOS 10.12, tvOS 10, *)) { + imageRef = ciImage.CGImage; + } + if (!imageRef) { + CIContext *context = [CIContext context]; + imageRef = [context createCGImage:ciImage fromRect:ciImage.extent]; + } + return imageRef; +} +#endif + @implementation UIImage (Transform) - (void)sd_drawInRect:(CGRect)rect context:(CGContextRef)context scaleMode:(SDImageScaleMode)scaleMode clipsToBounds:(BOOL)clips { @@ -400,7 +415,7 @@ static inline CIColor *SDCIColorConvertFromUIColor(UIColor * _Nonnull color) { // CIImage shortcut if (self.CIImage) { CIImage *ciImage = self.CIImage; - CIImage *colorImage = [CIImage imageWithColor:SDCIColorConvertFromUIColor(tintColor)]; + CIImage *colorImage = [CIImage imageWithColor:SDCIColorFromUIColor(tintColor)]; colorImage = [colorImage imageByCroppingToRect:ciImage.extent]; CIFilter *filter = [CIFilter filterWithName:@"CISourceAtopCompositing"]; [filter setValue:colorImage forKey:kCIInputImageKey]; @@ -435,19 +450,16 @@ static inline CIColor *SDCIColorConvertFromUIColor(UIColor * _Nonnull color) { } - (nullable UIColor *)sd_colorAtPoint:(CGPoint)point { - CGImageRef imageRef; + CGImageRef imageRef = NULL; // CIImage compatible +#if SD_UIKIT || SD_MAC if (self.CIImage) { - CIImage *ciImage = self.CIImage; - imageRef = ciImage.CGImage; - if (!imageRef) { - CIContext *context = [CIContext context]; - imageRef = [context createCGImage:ciImage fromRect:ciImage.extent]; - } - } else { + imageRef = SDCGImageFromCIImage(self.CIImage); + } +#endif + if (!imageRef) { imageRef = self.CGImage; } - if (!imageRef) { return nil; } @@ -488,19 +500,16 @@ static inline CIColor *SDCIColorConvertFromUIColor(UIColor * _Nonnull color) { } - (nullable NSArray *)sd_colorsWithRect:(CGRect)rect { - CGImageRef imageRef; - // CIImage shortcut + CGImageRef imageRef = NULL; + // CIImage compatible +#if SD_UIKIT || SD_MAC if (self.CIImage) { - CIImage *ciImage = self.CIImage; - imageRef = ciImage.CGImage; - if (!imageRef) { - CIContext *context = [CIContext context]; - imageRef = [context createCGImage:ciImage fromRect:ciImage.extent]; - } - } else { + imageRef = SDCGImageFromCIImage(self.CIImage); + } +#endif + if (!imageRef) { imageRef = self.CGImage; } - if (!imageRef) { return nil; }