//
//  CTImageCompression.m
//  Open
//
//  Created by 雷俊博 on 2019/11/19.
//  Copyright © 2019 黄江涛 . All rights reserved.
//

#import "CTImageCompression.h"

@implementation CTImageCompression

#pragma mark - 图片
/**  压缩图片*/
+ (UIImage *)imageWithOriginalImage:(UIImage *)image
{
    //宽高比
    CGFloat ratio = image.size.width/image.size.height;
    // 目标大小
    CGFloat targetW = 640;
    CGFloat targetH = 640;
    
    
    
    if (image.size.width<640 && image.size.height<640) {
        //宽高均 <= 1280, 图片尺寸保持不变
        return image;
    }else if (image.size.width > 640 && image.size.height > 640) {
        // 宽高均 > 1280 && 宽高比 > 2
        if (ratio>1) {
            // 宽大于高 取较小值(高)等于1280，较大值等比例压缩
            targetH = 640;
            targetW = targetH * ratio;
        }else{
            // 高大于宽 取较小值(宽)等于1280，较大值等比例压缩 (宽高比在0.5到2之间 )
            targetW = 640;
            targetH = targetW / ratio;
        }
    }else{
        // 宽或高 > 1280
        if (ratio>2) {
            // 宽图 图片尺寸大小保持不变
            targetW = image.size.width;
            targetH = image.size.height;
        }else if (ratio<0.5){
            // 长图 图片尺寸大小保持不变
            targetW = image.size.width;
            targetH = image.size.height;
        }else if (ratio > 1){
            // 宽大于高 取较大值(宽)等于1280，较小值等比例压缩
            targetW = 640;
            targetH = targetW / ratio;
        }else{
            // 高大于宽 取较大值(高)等于1280，较小值等比例压缩
            targetH = 640;
            targetW = targetH * ratio;
        }
    }
    
    image = [[CTImageCompression alloc] imageCompressWithImage:image targetHeight:targetH targetWidth:targetW];
    
    return image;
}

/**  重绘*/
- (UIImage *)imageCompressWithImage:(UIImage *)sourceImage targetHeight:(CGFloat)targetHeight targetWidth:(CGFloat)targetWidth
{
    //    CGFloat targetHeight = (targetWidth / sourceImage.size.width) * sourceImage.size.height;
    //UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight));
    UIGraphicsImageRenderer*re=[[UIGraphicsImageRenderer alloc] initWithSize:CGSizeMake(targetWidth, targetHeight)];
    [sourceImage drawInRect:CGRectMake(0,0,targetWidth, targetHeight)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}


/**  压缩图片 压缩质量 0 -- 1*/
+ (UIImage *)imageWithOriginalImage:(UIImage *)image quality:(CGFloat)quality{
    
    UIImage *newImage = [self imageWithOriginalImage:image];
    NSData *imageData = UIImageJPEGRepresentation(newImage, quality);
    return [UIImage imageWithData:imageData];
}


/**  压缩图片成Data*/
+ (NSData *)dataWithOriginalImage:(UIImage *)image
{
    return UIImageJPEGRepresentation([self imageWithOriginalImage:image], 1);
}

/**  压缩图片成Base64字符串*/
+ (NSString *)encodedImageToBase64StrWithImage:(UIImage *)image
{
    NSData *data = [self dataWithOriginalImage:image];
    
    NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    
    return encodedImageStr;
}
/**  Base64字符串成图片*/
+ (UIImage *)decodedBase64StrToImageWith:(NSString *)encodedImageStr
{
    NSData *decodedImageData = [[NSData alloc]initWithBase64EncodedString:encodedImageStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
    UIImage *decodedImage = [UIImage imageWithData:decodedImageData];
    return decodedImage;
}


@end
