//
//  UIImage+Category.m
//  Sleeper
//
//  Created by Luo yuntao on 15/7/16.
//  Copyright © 2019年 YJJ. All rights reserved.
//

#import "UIImage+Category.h"



#define MAX_SIZE  720
#define CYSreenshotFilePath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"screenshotDirectory"]
@implementation UIImage (Category)

+ (UIImage *)drawImageWithSize:(CGSize)size color:(UIColor *)color{
    
    UIImage *renderImage;
    UIView *renderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    renderView.backgroundColor = color;
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [renderView.layer renderInContext:context];
    renderImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return renderImage;
    
}

+(UIImage *)drawImageWithSize:(CGSize)size color:(UIColor *)color cornerRadius:(CGFloat)cornerRadius{
    UIImage *renderImage;
    UIView *renderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    renderView.backgroundColor = color;
    renderView.layer.cornerRadius = cornerRadius;
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [renderView.layer renderInContext:context];
    renderImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return renderImage;
}


+ (UIImage *)streImageNamed:(NSString *)imageName{
    if (!imageName) {
        return nil;
    }
    UIImage *image = [UIImage imageNamed:imageName];
    return [self streImageNamed:imageName capX:image.size.width/2 capY:image.size.height/2];
}

+ (UIImage *)streImageNamed:(NSString *)imageName capX:(CGFloat)x capY:(CGFloat)y{
    if (!imageName) {
        return nil;
    }
    UIImage *image = [UIImage imageNamed:imageName];
    return [image stretchableImageWithLeftCapWidth:x topCapHeight:y];
}




- (UIImage *)scaleImageWithSize:(CGSize)size{
    UIImage *image = nil;
    UIGraphicsBeginImageContext(CGSizeMake(size.width, size.height));
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

- (UIImage *)scaleImageWithScale:(CGFloat)scale{
    UIImage *image = nil;
    UIGraphicsBeginImageContext(CGSizeMake(self.size.width * scale, self.size.height * scale));
    [self drawInRect:CGRectMake(0, 0, self.size.width * scale, self.size.height * scale)];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

- (UIImage *)roundedCornerImageWithCornerRadius:(CGFloat)cornerRadius {
    CGFloat w = self.size.width;
    CGFloat h = self.size.height;
    CGFloat scale = [UIScreen mainScreen].scale;
    // 防止圆角半径小于0，或者大于宽/高中较小值的一半。
    if (cornerRadius < 0)
        cornerRadius = 0;
    else if (cornerRadius > MIN(w, h))
        cornerRadius = MIN(w, h) / 2.;
    
    UIImage *image = nil;
    CGRect imageFrame = CGRectMake(0., 0., w, h);
    UIGraphicsBeginImageContextWithOptions(self.size, NO, scale);
    [[UIBezierPath bezierPathWithRoundedRect:imageFrame cornerRadius:cornerRadius] addClip];
    [self drawInRect:imageFrame];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

- (UIImage *)drawImageFromImage:(UIImage*)originImage{
    
    CGSize size = [UIImage imageFitSize:originImage.size];
    UIImage *image = nil;
    UIGraphicsBeginImageContext(size);
    [originImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
    
}

//截当前频幕显示部分
+ (UIImage *)drawImageFromView:(UIView *)view
                          size:(CGSize)size{
    
    if (!view || CGSizeEqualToSize(size, CGSizeZero)) {
        return nil;
    }
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:context];
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultImage;

}
//截整个控制view包括未显示部分的两种方法
+ (UIImage *)drawImageFromTableView:(UITableView *)tableView{
    //方法一
//    UIGraphicsBeginImageContextWithOptions(tableView.contentSize, NO, [UIScreen mainScreen].scale);
//    tableView.frame = CGRectMake(0, 0, tableView.contentSize.width, tableView.contentSize.height);
//    [tableView.layer renderInContext:UIGraphicsGetCurrentContext()];
//    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    return image;
    
    //方法二
    UIImage* viewImage = nil;
    UITableView *scrollView = tableView;
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(KScreenWidth, scrollView.contentSize.height), scrollView.opaque, 0.0);
    {
        CGPoint savedContentOffset = scrollView.contentOffset;
        CGRect savedFrame = scrollView.frame;
        scrollView.contentOffset = CGPointZero;
        scrollView.frame = CGRectMake(0, 0, KScreenWidth, scrollView.contentSize.height);
        [scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
        viewImage = UIGraphicsGetImageFromCurrentImageContext();
        scrollView.contentOffset = savedContentOffset;
        scrollView.frame = savedFrame;
    }
    UIGraphicsEndImageContext();
    return viewImage;
}

+ (UIImage *)drawImageFromScrollView:(UIScrollView *)view{
    UIImage* image = nil;
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(KScreenWidth, view.contentSize.height), view.opaque, 0.0);//会比较清晰
    //UIGraphicsBeginImageContext(CGSizeMake(KScreenWidth, view.contentSize.height));
    {
        CGPoint savedContentOffset = view.contentOffset;
        CGRect savedFrame = view.frame;
        view.contentOffset = CGPointZero;
        view.frame = CGRectMake(0, 0, KScreenWidth, view.contentSize.height);
        [view.layer renderInContext: UIGraphicsGetCurrentContext()];
        image = UIGraphicsGetImageFromCurrentImageContext();
        view.contentOffset = savedContentOffset;
        view.frame = savedFrame;
    }
    UIGraphicsEndImageContext();
    
    return image;
}

+ (CGSize)imageFitSize:(CGSize)size{
    
    CGSize retSize = size;
    if (retSize.width == 0 || retSize.height == 0) {
        retSize.width = MAX_SIZE;
        retSize.height = MAX_SIZE;
    }
    if (retSize.width > retSize.height) {
        CGFloat height =  MAX_SIZE / retSize.width  *  retSize.height;
        retSize.height = height;
        retSize.width = MAX_SIZE;
    }else {
        CGFloat width = MAX_SIZE / retSize.height * retSize.width;
        retSize.width = width;
        retSize.height = MAX_SIZE;
    }
    return CGSizeMake(retSize.width ,retSize.height);
    
}

- (UIImage *)imageWithColor:(UIColor *)color;
{
    UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, self.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    CGContextClipToMask(context, rect, self.CGImage);
    [color setFill];
    CGContextFillRect(context, rect);
    UIImage*newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
+ (UIImage *)compressImageFromSourceImage:(UIImage *)image{
    CGSize imageSize = image.size;
    UIGraphicsBeginImageContext(CGSizeMake(imageSize.width, imageSize.height));
    [image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *data = UIImageJPEGRepresentation(newImage, 0.5);
    return [UIImage imageWithData:data];
}

+ (UIImage *)fp_resizableImage:(NSString *)imageName{
    UIImage * originImage = [UIImage imageNamed:imageName];
    CGFloat originImageWidth = originImage.size.width * 0.5;
    CGFloat originImageHeight = originImage.size.height * 0.5;
    UIImage * newImage = [originImage resizableImageWithCapInsets:UIEdgeInsetsMake(originImageHeight, originImageWidth, originImageHeight, originImageWidth) resizingMode:UIImageResizingModeStretch];
    return newImage;
}

+ (UIImage*)gradientImageWithBounds:(CGRect)bounds andColors:(NSArray*)colors andGradientType:(GradientDirection)gradientType{
    NSMutableArray *ar = [NSMutableArray array];
    
    for(UIColor *c in colors) {
        [ar addObject:(id)c.CGColor];
    }
    UIGraphicsBeginImageContextWithOptions(bounds.size, YES, 1);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]);
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, NULL);
    CGPoint startPt =  CGPointMake(0.0, 0.0);
    CGPoint endPt =  CGPointMake(0.0, 0.0);
    
    switch (gradientType) {
        case GradientDirectionTopToBottom:
            startPt= CGPointMake(0.0, 0.0);
            endPt= CGPointMake(0.0, bounds.size.height);
            break;
        case GradientDirectionLeftToRight:
            startPt = CGPointMake(0.0, 0.0);
            endPt = CGPointMake(bounds.size.width, 0.0);
            break;
        case GradientDirectionBottomToTop:
            startPt = CGPointMake(0.0, bounds.size.height);
            endPt = CGPointMake(0.0, 0.0);
            break;
        case GradientDirectionRightToLeft:
            startPt = CGPointMake(bounds.size.width, 0.0);
            endPt = CGPointMake(0, 0.0);
            break;
    }
    CGContextDrawLinearGradient(context, gradient, startPt, endPt, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    CGGradientRelease(gradient);
    CGContextRestoreGState(context);
    CGColorSpaceRelease(colorSpace);
    UIGraphicsEndImageContext();
    return image;
}
@end
