iOS: image render optimize

UIGraphicsBeginImageContextWithOptions replaced with UIGraphicsImageRenderer above iOS 10
This commit is contained in:
王劲鹏 2021-09-02 18:46:11 +08:00 committed by osborn
parent 627c107976
commit 7a0a2c6621
3 changed files with 106 additions and 40 deletions

View File

@ -147,15 +147,29 @@ - (UIImage *)currentPlaceHolderImage {
UIColor *color = DoricColor(self.placeHolderColor); UIColor *color = DoricColor(self.placeHolderColor);
CGRect rect = CGRectMake(0, 0, 1, 1); CGRect rect = CGRectMake(0, 0, 1, 1);
self.view.contentMode = UIViewContentModeScaleToFill; self.view.contentMode = UIViewContentModeScaleToFill;
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext(); if (@available(iOS 10.0, *)) {
CGContextSetFillColorWithColor(context, color.CGColor); UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
CGContextFillRect(context, rect); format.scale = [UIScreen mainScreen].scale;
UIGraphicsImageRenderer *render = [[UIGraphicsImageRenderer alloc]initWithSize:rect.size format:format];
UIImage *image = [render imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
CGContextRef context = rendererContext.CGContext;
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); CGContextSetFillColorWithColor(context, color.CGColor);
UIGraphicsEndImageContext(); CGContextFillRect(context, rect);
return image; }];
return image;
} else {
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
} }
return self.doricContext.driver.registry.defaultPlaceHolderImage; return self.doricContext.driver.registry.defaultPlaceHolderImage;
} }
@ -189,15 +203,29 @@ - (UIImage *)currentErrorImage {
UIColor *color = DoricColor(self.errorColor); UIColor *color = DoricColor(self.errorColor);
CGRect rect = CGRectMake(0, 0, 1, 1); CGRect rect = CGRectMake(0, 0, 1, 1);
self.view.contentMode = UIViewContentModeScaleToFill; self.view.contentMode = UIViewContentModeScaleToFill;
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext(); if (@available(iOS 10.0, *)) {
CGContextSetFillColorWithColor(context, color.CGColor); UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
CGContextFillRect(context, rect); format.scale = [UIScreen mainScreen].scale;
UIGraphicsImageRenderer *render = [[UIGraphicsImageRenderer alloc]initWithSize:rect.size format:format];
UIImage *image = [render imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
CGContextRef context = rendererContext.CGContext;
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); CGContextSetFillColorWithColor(context, color.CGColor);
UIGraphicsEndImageContext(); CGContextFillRect(context, rect);
return image; }];
return image;
} else {
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
} }
return self.doricContext.driver.registry.defaultErrorImage; return self.doricContext.driver.registry.defaultErrorImage;
} }

View File

@ -286,18 +286,37 @@ - (UIImage *)gradientImageFromColors:(NSArray *)colors
startPoint:(CGPoint)startPoint startPoint:(CGPoint)startPoint
endPoint:(CGPoint)endPoint endPoint:(CGPoint)endPoint
imgSize:(CGSize)imgSize { imgSize:(CGSize)imgSize {
UIGraphicsBeginImageContextWithOptions(imgSize, NO, [UIScreen mainScreen].scale); if (@available(iOS 10.0, *)) {
CGContextRef context = UIGraphicsGetCurrentContext(); UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
CGContextSaveGState(context); format.scale = [UIScreen mainScreen].scale;
CGColorSpaceRef colorSpace = CGColorGetColorSpace((__bridge CGColorRef) colors.lastObject); UIGraphicsImageRenderer *render = [[UIGraphicsImageRenderer alloc]initWithSize:imgSize format:format];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations); UIImage *image = [render imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
CGPoint start = (CGPoint) {startPoint.x * imgSize.width, startPoint.y * imgSize.height}; CGContextRef context = rendererContext.CGContext;
CGPoint end = (CGPoint) {endPoint.x * imgSize.width, endPoint.y * imgSize.height};
CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation); CGContextSaveGState(context);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); CGColorSpaceRef colorSpace = CGColorGetColorSpace((__bridge CGColorRef) colors.lastObject);
CGGradientRelease(gradient); CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);
CGContextRestoreGState(context); CGPoint start = (CGPoint) {startPoint.x * imgSize.width, startPoint.y * imgSize.height};
UIGraphicsEndImageContext(); CGPoint end = (CGPoint) {endPoint.x * imgSize.width, endPoint.y * imgSize.height};
return image; CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
CGGradientRelease(gradient);
CGContextRestoreGState(context);
}];
return image;
} else {
UIGraphicsBeginImageContextWithOptions(imgSize, NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGColorSpaceRef colorSpace = CGColorGetColorSpace((__bridge CGColorRef) colors.lastObject);
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);
CGPoint start = (CGPoint) {startPoint.x * imgSize.width, startPoint.y * imgSize.height};
CGPoint end = (CGPoint) {endPoint.x * imgSize.width, endPoint.y * imgSize.height};
CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
CGGradientRelease(gradient);
CGContextRestoreGState(context);
UIGraphicsEndImageContext();
return image;
}
} }
@end @end

View File

@ -340,19 +340,38 @@ - (UIImage *)gradientImageFromColors:(NSArray *)colors
startPoint:(CGPoint)startPoint startPoint:(CGPoint)startPoint
endPoint:(CGPoint)endPoint endPoint:(CGPoint)endPoint
imgSize:(CGSize)imgSize { imgSize:(CGSize)imgSize {
UIGraphicsBeginImageContextWithOptions(imgSize, NO, [UIScreen mainScreen].scale); if (@available(iOS 10.0, *)) {
CGContextRef context = UIGraphicsGetCurrentContext(); UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
CGContextSaveGState(context); format.scale = [UIScreen mainScreen].scale;
CGColorSpaceRef colorSpace = CGColorGetColorSpace((__bridge CGColorRef) colors.lastObject); UIGraphicsImageRenderer *render = [[UIGraphicsImageRenderer alloc]initWithSize:imgSize format:format];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations); UIImage *image = [render imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
CGPoint start = (CGPoint) {startPoint.x * imgSize.width, startPoint.y * imgSize.height}; CGContextRef context = rendererContext.CGContext;
CGPoint end = (CGPoint) {endPoint.x * imgSize.width, endPoint.y * imgSize.height};
CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation); CGContextSaveGState(context);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); CGColorSpaceRef colorSpace = CGColorGetColorSpace((__bridge CGColorRef) colors.lastObject);
CGGradientRelease(gradient); CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);
CGContextRestoreGState(context); CGPoint start = (CGPoint) {startPoint.x * imgSize.width, startPoint.y * imgSize.height};
UIGraphicsEndImageContext(); CGPoint end = (CGPoint) {endPoint.x * imgSize.width, endPoint.y * imgSize.height};
return image; CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
CGGradientRelease(gradient);
CGContextRestoreGState(context);
}];
return image;
} else {
UIGraphicsBeginImageContextWithOptions(imgSize, NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGColorSpaceRef colorSpace = CGColorGetColorSpace((__bridge CGColorRef) colors.lastObject);
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);
CGPoint start = (CGPoint) {startPoint.x * imgSize.width, startPoint.y * imgSize.height};
CGPoint end = (CGPoint) {endPoint.x * imgSize.width, endPoint.y * imgSize.height};
CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
CGGradientRelease(gradient);
CGContextRestoreGState(context);
UIGraphicsEndImageContext();
return image;
}
} }
- (void)requestLayout { - (void)requestLayout {