From f2f12951d94b7248dd6b09591f656f353c35feb9 Mon Sep 17 00:00:00 2001 From: jack <2478214664@qq.com> Date: Tue, 25 Jun 2024 15:47:23 +0800 Subject: [PATCH 1/2] Update YYAsyncLayer.m MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 兼容 iOS17 --- YYKit/Utility/YYAsyncLayer.m | 68 ++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 14 deletions(-) diff --git a/YYKit/Utility/YYAsyncLayer.m b/YYKit/Utility/YYAsyncLayer.m index 9a5c6dc5..b95849dc 100644 --- a/YYKit/Utility/YYAsyncLayer.m +++ b/YYKit/Utility/YYAsyncLayer.m @@ -188,32 +188,72 @@ - (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; + [_sentinel increase]; + if (task.willDisplay) task.willDisplay(self); + +CGSize size = self.bounds.size; +BOOL opaque = self.opaque; +CGFloat scale = self.contentsScale; +if (@available(iOS 10.0, *)) { + // 使用 UIGraphicsImageRenderer 进行绘制 + UIGraphicsImageRendererFormat *format = [UIGraphicsImageRendererFormat defaultFormat]; + format.opaque = opaque; + format.scale = scale; + + UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:size format:format]; + + UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext *rendererContext) { + CGContextRef context = rendererContext.CGContext; + if (opaque) { + CGSize scaledSize = self.bounds.size; + scaledSize.width *= self.contentsScale; + scaledSize.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)); + CGContextAddRect(context, CGRectMake(0, 0, scaledSize.width, scaledSize.height)); CGContextFillPath(context); } if (self.backgroundColor) { CGContextSetFillColorWithColor(context, self.backgroundColor); - CGContextAddRect(context, CGRectMake(0, 0, size.width, size.height)); + CGContextAddRect(context, CGRectMake(0, 0, scaledSize.width, scaledSize.height)); CGContextFillPath(context); } } CGContextRestoreGState(context); } task.display(context, self.bounds.size, ^{return NO;}); - UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); - UIGraphicsEndImageContext(); - self.contents = (__bridge id)(image.CGImage); - if (task.didDisplay) task.didDisplay(self, YES); + }]; + + self.contents = (__bridge id)(image.CGImage); +} else { + // 使用 UIGraphicsBeginImageContextWithOptions 进行绘制 + UIGraphicsBeginImageContextWithOptions(size, opaque, scale); + CGContextRef context = UIGraphicsGetCurrentContext(); + if (opaque) { + CGSize scaledSize = self.bounds.size; + scaledSize.width *= self.contentsScale; + scaledSize.height *= self.contentsScale; + CGContextSaveGState(context); { + if (!self.backgroundColor || CGColorGetAlpha(self.backgroundColor) < 1) { + CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); + CGContextAddRect(context, CGRectMake(0, 0, scaledSize.width, scaledSize.height)); + CGContextFillPath(context); + } + if (self.backgroundColor) { + CGContextSetFillColorWithColor(context, self.backgroundColor); + CGContextAddRect(context, CGRectMake(0, 0, scaledSize.width, scaledSize.height)); + CGContextFillPath(context); + } + } CGContextRestoreGState(context); + } + task.display(context, self.bounds.size, ^{return NO;}); + UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + + self.contents = (__bridge id)(image.CGImage); +} + +if (task.didDisplay) task.didDisplay(self, YES); } } From c21c5a1d5f7dac91dea926a6ed3b5795b03285ba Mon Sep 17 00:00:00 2001 From: jack <2478214664@qq.com> Date: Tue, 25 Jun 2024 15:52:57 +0800 Subject: [PATCH 2/2] Update YYAsyncLayer.m --- YYKit/Utility/YYAsyncLayer.m | 119 +++++++++++++++++------------------ 1 file changed, 59 insertions(+), 60 deletions(-) diff --git a/YYKit/Utility/YYAsyncLayer.m b/YYKit/Utility/YYAsyncLayer.m index b95849dc..0166c756 100644 --- a/YYKit/Utility/YYAsyncLayer.m +++ b/YYKit/Utility/YYAsyncLayer.m @@ -120,7 +120,7 @@ - (void)_displayAsync:(BOOL)async { if (task.willDisplay) task.willDisplay(self); YYSentinel *sentinel = _sentinel; int32_t value = sentinel.value; - BOOL (^isCancelled)() = ^BOOL() { + BOOL (^isCancelled)(void) = ^BOOL() { return value != sentinel.value; }; CGSize size = self.bounds.size; @@ -188,72 +188,71 @@ - (void)_displayAsync:(BOOL)async { }); }); } else { - [_sentinel increase]; - if (task.willDisplay) task.willDisplay(self); + [_sentinel increase]; + if (task.willDisplay) task.willDisplay(self); -CGSize size = self.bounds.size; -BOOL opaque = self.opaque; -CGFloat scale = self.contentsScale; -if (@available(iOS 10.0, *)) { - // 使用 UIGraphicsImageRenderer 进行绘制 - UIGraphicsImageRendererFormat *format = [UIGraphicsImageRendererFormat defaultFormat]; - format.opaque = opaque; - format.scale = scale; + CGSize size = self.bounds.size; + BOOL opaque = self.opaque; + CGFloat scale = self.contentsScale; + if (@available(iOS 10.0, *)) { + // 使用 UIGraphicsImageRenderer 进行绘制 + UIGraphicsImageRendererFormat *format = [UIGraphicsImageRendererFormat defaultFormat]; + format.opaque = opaque; + format.scale = scale; - UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:size format:format]; + UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:size format:format]; - UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext *rendererContext) { - CGContextRef context = rendererContext.CGContext; - if (opaque) { - CGSize scaledSize = self.bounds.size; - scaledSize.width *= self.contentsScale; - scaledSize.height *= self.contentsScale; - CGContextSaveGState(context); { - if (!self.backgroundColor || CGColorGetAlpha(self.backgroundColor) < 1) { - CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); - CGContextAddRect(context, CGRectMake(0, 0, scaledSize.width, scaledSize.height)); - CGContextFillPath(context); - } - if (self.backgroundColor) { - CGContextSetFillColorWithColor(context, self.backgroundColor); - CGContextAddRect(context, CGRectMake(0, 0, scaledSize.width, scaledSize.height)); - CGContextFillPath(context); + UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext *rendererContext) { + CGContextRef context = rendererContext.CGContext; + if (opaque) { + CGSize scaledSize = self.bounds.size; + scaledSize.width *= self.contentsScale; + scaledSize.height *= self.contentsScale; + CGContextSaveGState(context); { + if (!self.backgroundColor || CGColorGetAlpha(self.backgroundColor) < 1) { + CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); + CGContextAddRect(context, CGRectMake(0, 0, scaledSize.width, scaledSize.height)); + CGContextFillPath(context); + } + if (self.backgroundColor) { + CGContextSetFillColorWithColor(context, self.backgroundColor); + CGContextAddRect(context, CGRectMake(0, 0, scaledSize.width, scaledSize.height)); + CGContextFillPath(context); + } + } CGContextRestoreGState(context); } - } CGContextRestoreGState(context); - } - task.display(context, self.bounds.size, ^{return NO;}); - }]; + task.display(context, self.bounds.size, ^{return NO;}); + }]; - self.contents = (__bridge id)(image.CGImage); -} else { - // 使用 UIGraphicsBeginImageContextWithOptions 进行绘制 - UIGraphicsBeginImageContextWithOptions(size, opaque, scale); - CGContextRef context = UIGraphicsGetCurrentContext(); - if (opaque) { - CGSize scaledSize = self.bounds.size; - scaledSize.width *= self.contentsScale; - scaledSize.height *= self.contentsScale; - CGContextSaveGState(context); { - if (!self.backgroundColor || CGColorGetAlpha(self.backgroundColor) < 1) { - CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); - CGContextAddRect(context, CGRectMake(0, 0, scaledSize.width, scaledSize.height)); - CGContextFillPath(context); - } - if (self.backgroundColor) { - CGContextSetFillColorWithColor(context, self.backgroundColor); - CGContextAddRect(context, CGRectMake(0, 0, scaledSize.width, scaledSize.height)); - CGContextFillPath(context); + self.contents = (__bridge id)(image.CGImage); + } else { + // 使用 UIGraphicsBeginImageContextWithOptions 进行绘制 + UIGraphicsBeginImageContextWithOptions(size, opaque, scale); + CGContextRef context = UIGraphicsGetCurrentContext(); + if (opaque) { + CGSize scaledSize = self.bounds.size; + scaledSize.width *= self.contentsScale; + scaledSize.height *= self.contentsScale; + CGContextSaveGState(context); { + if (!self.backgroundColor || CGColorGetAlpha(self.backgroundColor) < 1) { + CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); + CGContextAddRect(context, CGRectMake(0, 0, scaledSize.width, scaledSize.height)); + CGContextFillPath(context); + } + if (self.backgroundColor) { + CGContextSetFillColorWithColor(context, self.backgroundColor); + CGContextAddRect(context, CGRectMake(0, 0, scaledSize.width, scaledSize.height)); + CGContextFillPath(context); + } + } CGContextRestoreGState(context); } - } CGContextRestoreGState(context); - } - task.display(context, self.bounds.size, ^{return NO;}); - UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); - UIGraphicsEndImageContext(); - - self.contents = (__bridge id)(image.CGImage); -} + task.display(context, self.bounds.size, ^{return NO;}); + UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); -if (task.didDisplay) task.didDisplay(self, YES); + self.contents = (__bridge id)(image.CGImage); + } + if (task.didDisplay) task.didDisplay(self, YES); } }