iOS: image render optimize
UIGraphicsBeginImageContextWithOptions replaced with UIGraphicsImageRenderer above iOS 10
This commit is contained in:
parent
627c107976
commit
7a0a2c6621
@ -147,6 +147,19 @@ - (UIImage *)currentPlaceHolderImage {
|
||||
UIColor *color = DoricColor(self.placeHolderColor);
|
||||
CGRect rect = CGRectMake(0, 0, 1, 1);
|
||||
self.view.contentMode = UIViewContentModeScaleToFill;
|
||||
|
||||
if (@available(iOS 10.0, *)) {
|
||||
UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
|
||||
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;
|
||||
|
||||
CGContextSetFillColorWithColor(context, color.CGColor);
|
||||
CGContextFillRect(context, rect);
|
||||
}];
|
||||
return image;
|
||||
} else {
|
||||
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
|
||||
|
||||
CGContextRef context = UIGraphicsGetCurrentContext();
|
||||
@ -157,6 +170,7 @@ - (UIImage *)currentPlaceHolderImage {
|
||||
UIGraphicsEndImageContext();
|
||||
return image;
|
||||
}
|
||||
}
|
||||
return self.doricContext.driver.registry.defaultPlaceHolderImage;
|
||||
}
|
||||
|
||||
@ -189,6 +203,19 @@ - (UIImage *)currentErrorImage {
|
||||
UIColor *color = DoricColor(self.errorColor);
|
||||
CGRect rect = CGRectMake(0, 0, 1, 1);
|
||||
self.view.contentMode = UIViewContentModeScaleToFill;
|
||||
|
||||
if (@available(iOS 10.0, *)) {
|
||||
UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
|
||||
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;
|
||||
|
||||
CGContextSetFillColorWithColor(context, color.CGColor);
|
||||
CGContextFillRect(context, rect);
|
||||
}];
|
||||
return image;
|
||||
} else {
|
||||
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
|
||||
|
||||
CGContextRef context = UIGraphicsGetCurrentContext();
|
||||
@ -199,6 +226,7 @@ - (UIImage *)currentErrorImage {
|
||||
UIGraphicsEndImageContext();
|
||||
return image;
|
||||
}
|
||||
}
|
||||
return self.doricContext.driver.registry.defaultErrorImage;
|
||||
}
|
||||
|
||||
|
@ -286,6 +286,24 @@ - (UIImage *)gradientImageFromColors:(NSArray *)colors
|
||||
startPoint:(CGPoint)startPoint
|
||||
endPoint:(CGPoint)endPoint
|
||||
imgSize:(CGSize)imgSize {
|
||||
if (@available(iOS 10.0, *)) {
|
||||
UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
|
||||
format.scale = [UIScreen mainScreen].scale;
|
||||
UIGraphicsImageRenderer *render = [[UIGraphicsImageRenderer alloc]initWithSize:imgSize format:format];
|
||||
UIImage *image = [render imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
|
||||
CGContextRef context = rendererContext.CGContext;
|
||||
|
||||
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);
|
||||
CGGradientRelease(gradient);
|
||||
CGContextRestoreGState(context);
|
||||
}];
|
||||
return image;
|
||||
} else {
|
||||
UIGraphicsBeginImageContextWithOptions(imgSize, NO, [UIScreen mainScreen].scale);
|
||||
CGContextRef context = UIGraphicsGetCurrentContext();
|
||||
CGContextSaveGState(context);
|
||||
@ -299,5 +317,6 @@ - (UIImage *)gradientImageFromColors:(NSArray *)colors
|
||||
CGContextRestoreGState(context);
|
||||
UIGraphicsEndImageContext();
|
||||
return image;
|
||||
}
|
||||
}
|
||||
@end
|
||||
|
@ -340,6 +340,24 @@ - (UIImage *)gradientImageFromColors:(NSArray *)colors
|
||||
startPoint:(CGPoint)startPoint
|
||||
endPoint:(CGPoint)endPoint
|
||||
imgSize:(CGSize)imgSize {
|
||||
if (@available(iOS 10.0, *)) {
|
||||
UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
|
||||
format.scale = [UIScreen mainScreen].scale;
|
||||
UIGraphicsImageRenderer *render = [[UIGraphicsImageRenderer alloc]initWithSize:imgSize format:format];
|
||||
UIImage *image = [render imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
|
||||
CGContextRef context = rendererContext.CGContext;
|
||||
|
||||
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);
|
||||
CGGradientRelease(gradient);
|
||||
CGContextRestoreGState(context);
|
||||
}];
|
||||
return image;
|
||||
} else {
|
||||
UIGraphicsBeginImageContextWithOptions(imgSize, NO, [UIScreen mainScreen].scale);
|
||||
CGContextRef context = UIGraphicsGetCurrentContext();
|
||||
CGContextSaveGState(context);
|
||||
@ -353,6 +371,7 @@ - (UIImage *)gradientImageFromColors:(NSArray *)colors
|
||||
CGContextRestoreGState(context);
|
||||
UIGraphicsEndImageContext();
|
||||
return image;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)requestLayout {
|
||||
|
Reference in New Issue
Block a user