Fix the compile issue on watchOS/macOS

This commit is contained in:
DreamPiggy 2019-12-15 16:36:13 +08:00
parent 6ff83fde6b
commit ee0aa220e0
1 changed files with 29 additions and 11 deletions

View File

@ -10,7 +10,9 @@
#import "SDImageGraphics.h" #import "SDImageGraphics.h"
@interface SDGraphicsImageRendererFormat () @interface SDGraphicsImageRendererFormat ()
@property (nonatomic, strong) UIGraphicsImageRendererFormat *uiformat API_AVAILABLE(ios(10.0)); #if SD_UIKIT
@property (nonatomic, strong) UIGraphicsImageRendererFormat *uiformat API_AVAILABLE(ios(10.0), tvos(10.0));
#endif
@end @end
@implementation SDGraphicsImageRendererFormat @implementation SDGraphicsImageRendererFormat
@ -18,12 +20,13 @@
- (instancetype)init { - (instancetype)init {
self = [super init]; self = [super init];
if (self) { if (self) {
if (@available(iOS 10.0, *)) { #if SD_UIKIT
if (@available(iOS 10.0, tvOS 10.10, *)) {
UIGraphicsImageRendererFormat *uiformat = [[UIGraphicsImageRendererFormat alloc] init]; UIGraphicsImageRendererFormat *uiformat = [[UIGraphicsImageRendererFormat alloc] init];
self.uiformat = uiformat; self.uiformat = uiformat;
self.scale = uiformat.scale; self.scale = uiformat.scale;
self.opaque = uiformat.opaque; self.opaque = uiformat.opaque;
if (@available(iOS 12.0, *)) { if (@available(iOS 12.0, tvOS 12.0, *)) {
self.preferredRange = (SDGraphicsImageRendererFormatRange)uiformat.preferredRange; self.preferredRange = (SDGraphicsImageRendererFormatRange)uiformat.preferredRange;
} else { } else {
if (uiformat.prefersExtendedRange) { if (uiformat.prefersExtendedRange) {
@ -33,21 +36,25 @@
} }
} }
} else { } else {
#endif
self.scale = 1.0; self.scale = 1.0;
self.opaque = NO; self.opaque = NO;
self.preferredRange = SDGraphicsImageRendererFormatRangeUnspecified; self.preferredRange = SDGraphicsImageRendererFormatRangeUnspecified;
#if SD_UIKIT
} }
#endif
} }
return self; return self;
} }
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability"
- (instancetype)initForMainScreen { - (instancetype)initForMainScreen {
self = [super init]; self = [super init];
if (self) { if (self) {
if (@available(iOS 10.0, *)) { #if SD_UIKIT
if (@available(iOS 10.0, tvOS 10.0, *)) {
UIGraphicsImageRendererFormat *uiformat; UIGraphicsImageRendererFormat *uiformat;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability"
// iOS 11.0.0 GM does have `preferredFormat`, but iOS 11 betas did not (argh!) // iOS 11.0.0 GM does have `preferredFormat`, but iOS 11 betas did not (argh!)
if ([UIGraphicsImageRenderer respondsToSelector:@selector(preferredFormat)]) { if ([UIGraphicsImageRenderer respondsToSelector:@selector(preferredFormat)]) {
uiformat = [UIGraphicsImageRendererFormat preferredFormat]; uiformat = [UIGraphicsImageRendererFormat preferredFormat];
@ -57,7 +64,7 @@
self.uiformat = uiformat; self.uiformat = uiformat;
self.scale = uiformat.scale; self.scale = uiformat.scale;
self.opaque = uiformat.opaque; self.opaque = uiformat.opaque;
if (@available(iOS 12.0, *)) { if (@available(iOS 12.0, tvOS 12.0, *)) {
self.preferredRange = (SDGraphicsImageRendererFormatRange)uiformat.preferredRange; self.preferredRange = (SDGraphicsImageRendererFormatRange)uiformat.preferredRange;
} else { } else {
if (uiformat.prefersExtendedRange) { if (uiformat.prefersExtendedRange) {
@ -66,8 +73,8 @@
self.preferredRange = SDGraphicsImageRendererFormatRangeStandard; self.preferredRange = SDGraphicsImageRendererFormatRangeStandard;
} }
} }
#pragma clang diagnostic pop
} else { } else {
#endif
#if SD_WATCH #if SD_WATCH
CGFloat screenScale = [WKInterfaceDevice currentDevice].screenScale; CGFloat screenScale = [WKInterfaceDevice currentDevice].screenScale;
#elif SD_UIKIT #elif SD_UIKIT
@ -78,10 +85,13 @@
self.scale = screenScale; self.scale = screenScale;
self.opaque = NO; self.opaque = NO;
self.preferredRange = SDGraphicsImageRendererFormatRangeUnspecified; self.preferredRange = SDGraphicsImageRendererFormatRangeUnspecified;
#if SD_UIKIT
} }
#endif
} }
return self; return self;
} }
#pragma clang diagnostic pop
+ (instancetype)preferredFormat { + (instancetype)preferredFormat {
SDGraphicsImageRendererFormat *format = [[SDGraphicsImageRendererFormat alloc] initForMainScreen]; SDGraphicsImageRendererFormat *format = [[SDGraphicsImageRendererFormat alloc] initForMainScreen];
@ -93,7 +103,9 @@
@interface SDGraphicsImageRenderer () @interface SDGraphicsImageRenderer ()
@property (nonatomic, assign) CGSize size; @property (nonatomic, assign) CGSize size;
@property (nonatomic, strong) SDGraphicsImageRendererFormat *format; @property (nonatomic, strong) SDGraphicsImageRendererFormat *format;
@property (nonatomic, strong) UIGraphicsImageRenderer *uirenderer API_AVAILABLE(ios(10.0)); #if SD_UIKIT
@property (nonatomic, strong) UIGraphicsImageRenderer *uirenderer API_AVAILABLE(ios(10.0), tvos(10.0));
#endif
@end @end
@implementation SDGraphicsImageRenderer @implementation SDGraphicsImageRenderer
@ -108,17 +120,20 @@
if (self) { if (self) {
self.size = size; self.size = size;
self.format = format; self.format = format;
if (@available(iOS 10.0, *)) { #if SD_UIKIT
if (@available(iOS 10.0, tvOS 10.0, *)) {
UIGraphicsImageRendererFormat *uiformat = format.uiformat; UIGraphicsImageRendererFormat *uiformat = format.uiformat;
self.uirenderer = [[UIGraphicsImageRenderer alloc] initWithSize:size format:uiformat]; self.uirenderer = [[UIGraphicsImageRenderer alloc] initWithSize:size format:uiformat];
} }
#endif
} }
return self; return self;
} }
- (UIImage *)imageWithActions:(NS_NOESCAPE SDGraphicsImageDrawingActions)actions { - (UIImage *)imageWithActions:(NS_NOESCAPE SDGraphicsImageDrawingActions)actions {
NSParameterAssert(actions); NSParameterAssert(actions);
if (@available(iOS 10.0, *)) { #if SD_UIKIT
if (@available(iOS 10.0, tvOS 10.0, *)) {
UIGraphicsImageDrawingActions uiactions = ^(UIGraphicsImageRendererContext *rendererContext) { UIGraphicsImageDrawingActions uiactions = ^(UIGraphicsImageRendererContext *rendererContext) {
if (actions) { if (actions) {
actions(rendererContext.CGContext); actions(rendererContext.CGContext);
@ -126,6 +141,7 @@
}; };
return [self.uirenderer imageWithActions:uiactions]; return [self.uirenderer imageWithActions:uiactions];
} else { } else {
#endif
SDGraphicsBeginImageContextWithOptions(self.size, self.format.opaque, self.format.scale); SDGraphicsBeginImageContextWithOptions(self.size, self.format.opaque, self.format.scale);
CGContextRef context = SDGraphicsGetCurrentContext(); CGContextRef context = SDGraphicsGetCurrentContext();
if (actions) { if (actions) {
@ -134,7 +150,9 @@
UIImage *image = SDGraphicsGetImageFromCurrentImageContext(); UIImage *image = SDGraphicsGetImageFromCurrentImageContext();
SDGraphicsEndImageContext(); SDGraphicsEndImageContext();
return image; return image;
#if SD_UIKIT
} }
#endif
} }
@end @end