//
//  UIImage+water.m
//  Gengmei
//
//  Created by Q14 on 2019/7/25.
//  Copyright © 2019 更美互动信息科技有限公司. All rights reserved.
//

#import "UIImage+water.h"

@implementation UIImage (water)
// 给图片添加图片水印
+ (UIImage *)wterImageWithImage:(UIImage *)image waterImage:(UIImage *)waterImage waterImageRect:(CGRect)rect {
    
    //开启上下文
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    //绘制背景图片
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    //绘制水印图片到当前上下文
    [waterImage drawInRect:rect];
    //从上下文中获取新图片
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    //关闭图形上下文
    UIGraphicsEndImageContext();
    //返回图片
    return newImage;
}

+ (UIImage *)waterMarkWithImage:(UIImage *)bgImage andMarkImageName:(UIImage *)waterImage {
//    bgImag.size = [UIImage getScreenImageSize:bgImage];
    UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0);
    [bgImage drawInRect:CGRectMake(0, 0, bgImage.size.width, bgImage.size.height)];
    CGFloat margin = 24;
    CGFloat waterW = waterImage.size.width;
    CGFloat waterH = waterImage.size.height;
    CGFloat scale = bgImage.size.width/UIScreen.mainScreen.bounds.size.width;
    CGFloat waterX = bgImage.size.width - waterW*scale - margin*scale;
    CGFloat waterY = bgImage.size.height - waterH*scale - margin*scale;
    [waterImage drawInRect:CGRectMake(waterX, waterY, waterW*scale, waterH*scale)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

+ (CGSize)getScreenImageSize:(UIImage *)image  {
    CGFloat scale1 = image.size.height / image.size.width;
    return  CGSizeMake(UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.width*scale1);
}
@end
