//
//  UIImage+Extension.m
//  SLAiELTS
//
//  Created by Gusont on 2023/3/10.
//

#import "UIImage+Extension.h"

@implementation UIImage (Extension)

+ (NSData *)compressImage:(UIImage *)image {
    CGFloat scale = image.size.height / image.size.width;
    CGSize size = CGSizeMake(240, 240*scale);
//    DYJLog(@"imagesize:%@ oldimage:%ld", NSStringFromCGSize(size), (unsigned long)UIImagePNGRepresentation(image).length/1024);
    UIGraphicsBeginImageContextWithOptions(size, YES, 0);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    NSData *imagedata = UIImagePNGRepresentation(smallImage);
//    DYJLog(@"newImage:%ld", (unsigned long)imagedata.length/1024);
    CGFloat compereScale = 0.9;
    // 将图片压至小于200kb
    while (200*1024.0 < imagedata.length) {
        imagedata = UIImageJPEGRepresentation(smallImage, compereScale);
        compereScale -= 0.1;
//        DYJLog(@"scaleimage:%ld", (unsigned long)imagedata.length/1024);
    }
    return imagedata;
}


//纯色图
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage*theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

//纯色图-圆角
+ (UIImage *)imageWithColor:(UIColor *)color
                       size:(CGSize)size
               cornerRadius:(CGFloat)cornerRadius
{
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    if (cornerRadius > 0) {
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, size.width, size.height) cornerRadius:cornerRadius];
        [path addClip];
    }
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage*theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

//渐变图
+ (UIImage *)gradientImageSize:(CGSize)size
                    startPoint:(CGPoint)startPoint
                      endPoint:(CGPoint)endPoint
                        colors:(NSArray <UIColor *> *)colorStrs
{
    return [self gradientImageSize:size startPoint:startPoint endPoint:endPoint colors:colorStrs cornerRadius:0];
}

//渐变图-圆角
+ (UIImage *)gradientImageSize:(CGSize)size
                    startPoint:(CGPoint)startPoint
                      endPoint:(CGPoint)endPoint
                        colors:(NSArray <UIColor *> *)colorStrs
                  cornerRadius:(CGFloat)cornerRadius
{
    if (!colorStrs || colorStrs.count <= 0) {
        return nil;
    }
    NSMutableArray *gradientColors = [NSMutableArray arrayWithCapacity:colorStrs.count + 1];
    for (int i = 0; i < colorStrs.count; i++) {
        UIColor *color = colorStrs[i];
        if (color) {
            [gradientColors addObject:(id)color.CGColor];
        }
    }
    
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    if (cornerRadius > 0) {
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, size.width, size.height) cornerRadius:cornerRadius];
        [path addClip];
    }
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColors(rgb, (__bridge CFArrayRef)gradientColors, NULL);
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
    CGGradientRelease(gradient);
    CGColorSpaceRelease(rgb);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

// 字符串转图片
+ (UIImage *)imageFromBase64String:(NSString *)string
{
    if (string) {
        NSString *imgStr = [string componentsSeparatedByString:@";base64,"].lastObject;
        // NSString --> NSData
        NSData *data=[[NSData alloc] initWithBase64EncodedString:imgStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
        // NSData --> UIImage
        UIImage *image = [UIImage imageWithData:data];
        return image;
    }
    return nil;
}

@end
