Refactory the `sd_flippedImageWithHorizontal` with the UIGraphicsRenderer, do not always need ARGB8888

This commit is contained in:
DreamPiggy 2019-12-15 20:20:38 +08:00
parent 48a7b7f943
commit 1e778f0fe6
1 changed files with 18 additions and 30 deletions

View File

@ -280,37 +280,25 @@ static inline UIColor * SDGetColorFromPixel(Pixel_8888 pixel, CGBitmapInfo bitma
- (nullable UIImage *)sd_flippedImageWithHorizontal:(BOOL)horizontal vertical:(BOOL)vertical { - (nullable UIImage *)sd_flippedImageWithHorizontal:(BOOL)horizontal vertical:(BOOL)vertical {
if (!self.CGImage) return nil; if (!self.CGImage) return nil;
size_t width = (size_t)CGImageGetWidth(self.CGImage); size_t width = self.size.width;
size_t height = (size_t)CGImageGetHeight(self.CGImage); size_t height = self.size.height;
size_t bytesPerRow = width * 4;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(colorSpace);
if (!context) return nil;
CGContextDrawImage(context, CGRectMake(0, 0, width, height), self.CGImage); SDGraphicsImageRendererFormat *format = [[SDGraphicsImageRendererFormat alloc] init];
UInt8 *data = (UInt8 *)CGBitmapContextGetData(context); format.scale = self.scale;
if (!data) { SDGraphicsImageRenderer *renderer = [[SDGraphicsImageRenderer alloc] initWithSize:self.size format:format];
CGContextRelease(context); UIImage *image = [renderer imageWithActions:^(CGContextRef _Nonnull context) {
return nil; // Use UIKit coordinate system
}
vImage_Buffer src = { data, height, width, bytesPerRow };
vImage_Buffer dest = { data, height, width, bytesPerRow };
if (vertical) {
vImageVerticalReflect_ARGB8888(&src, &dest, kvImageBackgroundColorFill);
}
if (horizontal) { if (horizontal) {
vImageHorizontalReflect_ARGB8888(&src, &dest, kvImageBackgroundColorFill); CGAffineTransform flipHorizontal = CGAffineTransformMake(-1, 0, 0, 1, width, 0);
CGContextConcatCTM(context, flipHorizontal);
} }
CGImageRef imgRef = CGBitmapContextCreateImage(context); if (vertical) {
CGContextRelease(context); CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, height);
#if SD_UIKIT || SD_WATCH CGContextConcatCTM(context, flipVertical);
UIImage *img = [UIImage imageWithCGImage:imgRef scale:self.scale orientation:self.imageOrientation]; }
#else [self drawInRect:CGRectMake(0, 0, width, height)];
UIImage *img = [[UIImage alloc] initWithCGImage:imgRef scale:self.scale orientation:kCGImagePropertyOrientationUp]; }];
#endif return image;
CGImageRelease(imgRef);
return img;
} }
#pragma mark - Image Blending #pragma mark - Image Blending