Replace the SDGraphicsBeginImageContextWithOptions with SDGraphicsImageRenderer
This commit is contained in:
parent
ee0aa220e0
commit
8fa6c7519c
|
@ -9,6 +9,7 @@
|
||||||
#import "UIImage+Transform.h"
|
#import "UIImage+Transform.h"
|
||||||
#import "NSImage+Compatibility.h"
|
#import "NSImage+Compatibility.h"
|
||||||
#import "SDImageGraphics.h"
|
#import "SDImageGraphics.h"
|
||||||
|
#import "SDGraphicsImageRenderer.h"
|
||||||
#import "NSBezierPath+RoundedCorners.h"
|
#import "NSBezierPath+RoundedCorners.h"
|
||||||
#import <Accelerate/Accelerate.h>
|
#import <Accelerate/Accelerate.h>
|
||||||
#if SD_UIKIT || SD_MAC
|
#if SD_UIKIT || SD_MAC
|
||||||
|
@ -165,11 +166,10 @@ static inline UIColor * SDGetColorFromPixel(Pixel_8888 pixel, CGBitmapInfo bitma
|
||||||
|
|
||||||
@implementation UIImage (Transform)
|
@implementation UIImage (Transform)
|
||||||
|
|
||||||
- (void)sd_drawInRect:(CGRect)rect withScaleMode:(SDImageScaleMode)scaleMode clipsToBounds:(BOOL)clips {
|
- (void)sd_drawInRect:(CGRect)rect context:(CGContextRef)context scaleMode:(SDImageScaleMode)scaleMode clipsToBounds:(BOOL)clips {
|
||||||
CGRect drawRect = SDCGRectFitWithScaleMode(rect, self.size, scaleMode);
|
CGRect drawRect = SDCGRectFitWithScaleMode(rect, self.size, scaleMode);
|
||||||
if (drawRect.size.width == 0 || drawRect.size.height == 0) return;
|
if (drawRect.size.width == 0 || drawRect.size.height == 0) return;
|
||||||
if (clips) {
|
if (clips) {
|
||||||
CGContextRef context = SDGraphicsGetCurrentContext();
|
|
||||||
if (context) {
|
if (context) {
|
||||||
CGContextSaveGState(context);
|
CGContextSaveGState(context);
|
||||||
CGContextAddRect(context, rect);
|
CGContextAddRect(context, rect);
|
||||||
|
@ -184,10 +184,12 @@ static inline UIColor * SDGetColorFromPixel(Pixel_8888 pixel, CGBitmapInfo bitma
|
||||||
|
|
||||||
- (nullable UIImage *)sd_resizedImageWithSize:(CGSize)size scaleMode:(SDImageScaleMode)scaleMode {
|
- (nullable UIImage *)sd_resizedImageWithSize:(CGSize)size scaleMode:(SDImageScaleMode)scaleMode {
|
||||||
if (size.width <= 0 || size.height <= 0) return nil;
|
if (size.width <= 0 || size.height <= 0) return nil;
|
||||||
SDGraphicsBeginImageContextWithOptions(size, NO, self.scale);
|
SDGraphicsImageRendererFormat *format = [[SDGraphicsImageRendererFormat alloc] init];
|
||||||
[self sd_drawInRect:CGRectMake(0, 0, size.width, size.height) withScaleMode:scaleMode clipsToBounds:NO];
|
format.scale = self.scale;
|
||||||
UIImage *image = SDGraphicsGetImageFromCurrentImageContext();
|
SDGraphicsImageRenderer *renderer = [[SDGraphicsImageRenderer alloc] initWithSize:size format:format];
|
||||||
SDGraphicsEndImageContext();
|
UIImage *image = [renderer imageWithActions:^(CGContextRef _Nonnull context) {
|
||||||
|
[self sd_drawInRect:CGRectMake(0, 0, size.width, size.height) context:context scaleMode:scaleMode clipsToBounds:NO];
|
||||||
|
}];
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -213,43 +215,43 @@ static inline UIColor * SDGetColorFromPixel(Pixel_8888 pixel, CGBitmapInfo bitma
|
||||||
|
|
||||||
- (nullable UIImage *)sd_roundedCornerImageWithRadius:(CGFloat)cornerRadius corners:(SDRectCorner)corners borderWidth:(CGFloat)borderWidth borderColor:(nullable UIColor *)borderColor {
|
- (nullable UIImage *)sd_roundedCornerImageWithRadius:(CGFloat)cornerRadius corners:(SDRectCorner)corners borderWidth:(CGFloat)borderWidth borderColor:(nullable UIColor *)borderColor {
|
||||||
if (!self.CGImage) return nil;
|
if (!self.CGImage) return nil;
|
||||||
SDGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
|
SDGraphicsImageRendererFormat *format = [[SDGraphicsImageRendererFormat alloc] init];
|
||||||
CGContextRef context = SDGraphicsGetCurrentContext();
|
format.scale = self.scale;
|
||||||
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
|
SDGraphicsImageRenderer *renderer = [[SDGraphicsImageRenderer alloc] initWithSize:self.size format:format];
|
||||||
|
UIImage *image = [renderer imageWithActions:^(CGContextRef _Nonnull context) {
|
||||||
CGFloat minSize = MIN(self.size.width, self.size.height);
|
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
|
||||||
if (borderWidth < minSize / 2) {
|
|
||||||
#if SD_UIKIT || SD_WATCH
|
|
||||||
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(rect, borderWidth, borderWidth) byRoundingCorners:corners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
|
|
||||||
#else
|
|
||||||
NSBezierPath *path = [NSBezierPath sd_bezierPathWithRoundedRect:CGRectInset(rect, borderWidth, borderWidth) byRoundingCorners:corners cornerRadius:cornerRadius];
|
|
||||||
#endif
|
|
||||||
[path closePath];
|
|
||||||
|
|
||||||
CGContextSaveGState(context);
|
CGFloat minSize = MIN(self.size.width, self.size.height);
|
||||||
[path addClip];
|
if (borderWidth < minSize / 2) {
|
||||||
[self drawInRect:rect];
|
|
||||||
CGContextRestoreGState(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (borderColor && borderWidth < minSize / 2 && borderWidth > 0) {
|
|
||||||
CGFloat strokeInset = (floor(borderWidth * self.scale) + 0.5) / self.scale;
|
|
||||||
CGRect strokeRect = CGRectInset(rect, strokeInset, strokeInset);
|
|
||||||
CGFloat strokeRadius = cornerRadius > self.scale / 2 ? cornerRadius - self.scale / 2 : 0;
|
|
||||||
#if SD_UIKIT || SD_WATCH
|
#if SD_UIKIT || SD_WATCH
|
||||||
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:strokeRect byRoundingCorners:corners cornerRadii:CGSizeMake(strokeRadius, strokeRadius)];
|
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(rect, borderWidth, borderWidth) byRoundingCorners:corners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
|
||||||
#else
|
#else
|
||||||
NSBezierPath *path = [NSBezierPath sd_bezierPathWithRoundedRect:strokeRect byRoundingCorners:corners cornerRadius:strokeRadius];
|
NSBezierPath *path = [NSBezierPath sd_bezierPathWithRoundedRect:CGRectInset(rect, borderWidth, borderWidth) byRoundingCorners:corners cornerRadius:cornerRadius];
|
||||||
#endif
|
#endif
|
||||||
[path closePath];
|
[path closePath];
|
||||||
|
|
||||||
|
CGContextSaveGState(context);
|
||||||
|
[path addClip];
|
||||||
|
[self drawInRect:rect];
|
||||||
|
CGContextRestoreGState(context);
|
||||||
|
}
|
||||||
|
|
||||||
path.lineWidth = borderWidth;
|
if (borderColor && borderWidth < minSize / 2 && borderWidth > 0) {
|
||||||
[borderColor setStroke];
|
CGFloat strokeInset = (floor(borderWidth * self.scale) + 0.5) / self.scale;
|
||||||
[path stroke];
|
CGRect strokeRect = CGRectInset(rect, strokeInset, strokeInset);
|
||||||
}
|
CGFloat strokeRadius = cornerRadius > self.scale / 2 ? cornerRadius - self.scale / 2 : 0;
|
||||||
|
#if SD_UIKIT || SD_WATCH
|
||||||
UIImage *image = SDGraphicsGetImageFromCurrentImageContext();
|
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:strokeRect byRoundingCorners:corners cornerRadii:CGSizeMake(strokeRadius, strokeRadius)];
|
||||||
SDGraphicsEndImageContext();
|
#else
|
||||||
|
NSBezierPath *path = [NSBezierPath sd_bezierPathWithRoundedRect:strokeRect byRoundingCorners:corners cornerRadius:strokeRadius];
|
||||||
|
#endif
|
||||||
|
[path closePath];
|
||||||
|
|
||||||
|
path.lineWidth = borderWidth;
|
||||||
|
[borderColor setStroke];
|
||||||
|
[path stroke];
|
||||||
|
}
|
||||||
|
}];
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -347,15 +349,15 @@ static inline UIColor * SDGetColorFromPixel(Pixel_8888 pixel, CGBitmapInfo bitma
|
||||||
// blend mode, see https://en.wikipedia.org/wiki/Alpha_compositing
|
// blend mode, see https://en.wikipedia.org/wiki/Alpha_compositing
|
||||||
CGBlendMode blendMode = kCGBlendModeSourceAtop;
|
CGBlendMode blendMode = kCGBlendModeSourceAtop;
|
||||||
|
|
||||||
SDGraphicsBeginImageContextWithOptions(size, NO, scale);
|
SDGraphicsImageRendererFormat *format = [[SDGraphicsImageRendererFormat alloc] init];
|
||||||
CGContextRef context = SDGraphicsGetCurrentContext();
|
format.scale = scale;
|
||||||
[self drawInRect:rect];
|
SDGraphicsImageRenderer *renderer = [[SDGraphicsImageRenderer alloc] initWithSize:size format:format];
|
||||||
CGContextSetBlendMode(context, blendMode);
|
UIImage *image = [renderer imageWithActions:^(CGContextRef _Nonnull context) {
|
||||||
CGContextSetFillColorWithColor(context, tintColor.CGColor);
|
[self drawInRect:rect];
|
||||||
CGContextFillRect(context, rect);
|
CGContextSetBlendMode(context, blendMode);
|
||||||
UIImage *image = SDGraphicsGetImageFromCurrentImageContext();
|
CGContextSetFillColorWithColor(context, tintColor.CGColor);
|
||||||
SDGraphicsEndImageContext();
|
CGContextFillRect(context, rect);
|
||||||
|
}];
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue