From ffe4a4c72b4aaf7969a49c62c267e4a1eb88fa4b Mon Sep 17 00:00:00 2001 From: Caio Lima Date: Tue, 1 Aug 2023 13:26:42 -0400 Subject: [PATCH] Modernize BVLinearGradientLayer to use UIGraphicsImageRenderer if on supported OS versions --- ios/BVLinearGradientLayer.m | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/ios/BVLinearGradientLayer.m b/ios/BVLinearGradientLayer.m index 5049d6a..bda0a1b 100644 --- a/ios/BVLinearGradientLayer.m +++ b/ios/BVLinearGradientLayer.m @@ -60,15 +60,32 @@ - (void)display { hasAlpha = hasAlpha || CGColorGetAlpha(self.colors[i].CGColor) < 1.0; } - UIGraphicsBeginImageContextWithOptions(self.bounds.size, !hasAlpha, 0.0); - CGContextRef ref = UIGraphicsGetCurrentContext(); - [self drawInContext:ref]; + if (@available(iOS 10.0, *)) { + UIGraphicsImageRendererFormat *format; + if (@available(iOS 11.0, *)) { + format = [UIGraphicsImageRendererFormat preferredFormat]; + } else { + format = [UIGraphicsImageRendererFormat defaultFormat]; + } + format.opaque = !hasAlpha; + UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:self.bounds.size format:format]; + UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull ref) { + [self drawInContext:ref.CGContext]; + }]; + + self.contents = (__bridge id _Nullable)(image.CGImage); + self.contentsScale = image.scale; + } else { + UIGraphicsBeginImageContextWithOptions(self.bounds.size, !hasAlpha, 0.0); + CGContextRef ref = UIGraphicsGetCurrentContext(); + [self drawInContext:ref]; - UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); - self.contents = (__bridge id _Nullable)(image.CGImage); - self.contentsScale = image.scale; + UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); + self.contents = (__bridge id _Nullable)(image.CGImage); + self.contentsScale = image.scale; - UIGraphicsEndImageContext(); + UIGraphicsEndImageContext(); + } } - (void)setUseAngle:(BOOL)useAngle