diff --git a/Demo/YYKitDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Demo/YYKitDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 00000000..18d98100 --- /dev/null +++ b/Demo/YYKitDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/YYKit/Base/Quartz/CALayer+YYAdd.m b/YYKit/Base/Quartz/CALayer+YYAdd.m index 2f6b1c0a..8ebfe793 100644 --- a/YYKit/Base/Quartz/CALayer+YYAdd.m +++ b/YYKit/Base/Quartz/CALayer+YYAdd.m @@ -19,6 +19,7 @@ @implementation CALayer (YYAdd) - (UIImage *)snapshotImage { + if (self.bounds.size.width <= 0 || self.bounds.size.height <= 0) return nil; UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0); CGContextRef context = UIGraphicsGetCurrentContext(); [self renderInContext:context]; diff --git a/YYKit/Base/UIKit/UIImage+YYAdd.m b/YYKit/Base/UIKit/UIImage+YYAdd.m index 357d3106..53d057b1 100644 --- a/YYKit/Base/UIKit/UIImage+YYAdd.m +++ b/YYKit/Base/UIKit/UIImage+YYAdd.m @@ -264,6 +264,7 @@ + (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size { + (UIImage *)imageWithSize:(CGSize)size drawBlock:(void (^)(CGContextRef context))drawBlock { if (!drawBlock) return nil; + if (size.width <= 0 || size.height <= 0) return nil; UIGraphicsBeginImageContextWithOptions(size, NO, 0); CGContextRef context = UIGraphicsGetCurrentContext(); if (!context) return nil; @@ -371,6 +372,7 @@ - (UIImage *)imageByRoundCornerRadius:(CGFloat)radius borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor borderLineJoin:(CGLineJoin)borderLineJoin { + if (self.size.width <= 0 || self.size.height <= 0) return nil; if (corners != UIRectCornerAllCorners) { UIRectCorner tmp = 0; @@ -500,6 +502,7 @@ - (UIImage *)imageByFlipHorizontal { } - (UIImage *)imageByTintColor:(UIColor *)color { + if (self.size.width <= 0 || self.size.height <= 0) return nil; UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale); CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height); [color set]; @@ -714,6 +717,7 @@ - (UIImage *)_yy_mergeImageRef:(CGImageRef)effectCGImage tintBlendMode:(CGBlendMode)tintBlendMode maskImage:(UIImage *)maskImage opaque:(BOOL)opaque { + if (self.size.width <= 0 || self.size.height <= 0) return nil; BOOL hasTint = tintColor != nil && CGColorGetAlpha(tintColor.CGColor) > __FLT_EPSILON__; BOOL hasMask = maskImage != nil; CGSize size = self.size; diff --git a/YYKit/Base/UIKit/UIView+YYAdd.m b/YYKit/Base/UIKit/UIView+YYAdd.m index 36f9e83c..3d0bb95f 100644 --- a/YYKit/Base/UIKit/UIView+YYAdd.m +++ b/YYKit/Base/UIKit/UIView+YYAdd.m @@ -19,6 +19,7 @@ @implementation UIView (YYAdd) - (UIImage *)snapshotImage { + if (self.bounds.size.width <= 0 || self.bounds.size.height <= 0) return nil; UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0); [self.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *snap = UIGraphicsGetImageFromCurrentImageContext(); @@ -30,6 +31,7 @@ - (UIImage *)snapshotImageAfterScreenUpdates:(BOOL)afterUpdates { if (![self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) { return [self snapshotImage]; } + if (self.bounds.size.width <= 0 || self.bounds.size.height <= 0) return nil; UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0); [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:afterUpdates]; UIImage *snap = UIGraphicsGetImageFromCurrentImageContext(); diff --git a/YYKit/Utility/YYAsyncLayer.m b/YYKit/Utility/YYAsyncLayer.m index 9a5c6dc5..9036fdb6 100644 --- a/YYKit/Utility/YYAsyncLayer.m +++ b/YYKit/Utility/YYAsyncLayer.m @@ -145,33 +145,32 @@ - (void)_displayAsync:(BOOL)async { CGColorRelease(backgroundColor); return; } - UIGraphicsBeginImageContextWithOptions(size, opaque, scale); - CGContextRef context = UIGraphicsGetCurrentContext(); - if (opaque && context) { - CGContextSaveGState(context); { - if (!backgroundColor || CGColorGetAlpha(backgroundColor) < 1) { - CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); - CGContextAddRect(context, CGRectMake(0, 0, size.width * scale, size.height * scale)); - CGContextFillPath(context); - } - if (backgroundColor) { - CGContextSetFillColorWithColor(context, backgroundColor); - CGContextAddRect(context, CGRectMake(0, 0, size.width * scale, size.height * scale)); - CGContextFillPath(context); - } - } CGContextRestoreGState(context); - CGColorRelease(backgroundColor); - } - task.display(context, size, isCancelled); - if (isCancelled()) { - UIGraphicsEndImageContext(); - dispatch_async(dispatch_get_main_queue(), ^{ - if (task.didDisplay) task.didDisplay(self, NO); - }); - return; - } - UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); - UIGraphicsEndImageContext(); + + UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:size]; + UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull context) { + if (opaque) { + CGContextSaveGState(context.CGContext); { + if (!backgroundColor || CGColorGetAlpha(backgroundColor) < 1) { + CGContextSetFillColorWithColor(context.CGContext, [UIColor whiteColor].CGColor); + CGContextAddRect(context.CGContext, CGRectMake(0, 0, size.width * scale, size.height * scale)); + CGContextFillPath(context.CGContext); + } + if (backgroundColor) { + CGContextSetFillColorWithColor(context.CGContext, backgroundColor); + CGContextAddRect(context.CGContext, CGRectMake(0, 0, size.width * scale, size.height * scale)); + CGContextFillPath(context.CGContext); + } + } CGContextRestoreGState(context.CGContext); + CGColorRelease(backgroundColor); + } + task.display(context.CGContext, size, isCancelled); + if (isCancelled()) { + dispatch_async(dispatch_get_main_queue(), ^{ + if (task.didDisplay) task.didDisplay(self, NO); + }); + return; + } + }]; if (isCancelled()) { dispatch_async(dispatch_get_main_queue(), ^{ if (task.didDisplay) task.didDisplay(self, NO); @@ -190,28 +189,29 @@ - (void)_displayAsync:(BOOL)async { } else { [_sentinel increase]; if (task.willDisplay) task.willDisplay(self); - UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, self.contentsScale); - CGContextRef context = UIGraphicsGetCurrentContext(); - if (self.opaque && context) { - CGSize size = self.bounds.size; - size.width *= self.contentsScale; - size.height *= self.contentsScale; - CGContextSaveGState(context); { - if (!self.backgroundColor || CGColorGetAlpha(self.backgroundColor) < 1) { - CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); - CGContextAddRect(context, CGRectMake(0, 0, size.width, size.height)); - CGContextFillPath(context); - } - if (self.backgroundColor) { - CGContextSetFillColorWithColor(context, self.backgroundColor); - CGContextAddRect(context, CGRectMake(0, 0, size.width, size.height)); - CGContextFillPath(context); - } - } CGContextRestoreGState(context); - } - task.display(context, self.bounds.size, ^{return NO;}); - UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); - UIGraphicsEndImageContext(); + CGSize size = self.bounds.size; + BOOL opaque = self.opaque; + CGFloat scale = self.contentsScale; + CGColorRef backgroundColor = (opaque && self.backgroundColor) ? CGColorRetain(self.backgroundColor) : NULL; + UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:size]; + UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull context) { + if (opaque) { + CGContextSaveGState(context.CGContext); { + if (!backgroundColor || CGColorGetAlpha(backgroundColor) < 1) { + CGContextSetFillColorWithColor(context.CGContext, [UIColor whiteColor].CGColor); + CGContextAddRect(context.CGContext, CGRectMake(0, 0, size.width * scale, size.height * scale)); + CGContextFillPath(context.CGContext); + } + if (backgroundColor) { + CGContextSetFillColorWithColor(context.CGContext, backgroundColor); + CGContextAddRect(context.CGContext, CGRectMake(0, 0, size.width * scale, size.height * scale)); + CGContextFillPath(context.CGContext); + } + } CGContextRestoreGState(context.CGContext); + CGColorRelease(backgroundColor); + } + task.display((__bridge CGContextRef _Nonnull)(context), self.bounds.size, ^{return NO;}); + }]; self.contents = (__bridge id)(image.CGImage); if (task.didDisplay) task.didDisplay(self, YES); }