图片水印和裁剪

  • 效果图:
图片水印和裁剪_第1张图片
图1

图片水印和裁剪_第2张图片
图2

图片水印和裁剪_第3张图片
图3
  • 主要代码

// UIImage+image.h

#import 

@interface UIImage (image)
/**
 *  返回一个带有边框的圆形裁剪图片
 *
 *  @param borderW    边框宽度
 *  @param boderColor 边框颜色
 *  @param oriImage   要裁剪的图片
 *
 *  @return 已经裁剪好的带有边框的图片
 */
+ (UIImage *)imageWithBorder:(CGFloat)borderW color:(UIColor *)boderColor image:(UIImage *)oriImage;

/**
 *  返回一个已经添加好文字的图片
 *
 *  @param oriString    要添加的文字
 *  @param oriImage   要裁剪的图片
 *
 *  @return 已经添加好文字的图片
 */
+ (UIImage *)imageWithString:(NSString *)oriString image:(UIImage *)oriImage;

/**
 *  返回一个已经裁剪好的图片

 *  @param oriImage   要裁剪的图片
 *
 *  @return 已经裁剪好的图片
 */
+ (UIImage *)imageWithOriImage:(UIImage *)oriImage;

@end



// UIImage+image.m


#import "UIImage+image.h"

@implementation UIImage (image)

+ (UIImage *)imageWithBorder:(CGFloat)borderW color:(UIColor *)boderColor image:(UIImage *)oriImage {


    //1.确定边框的宽度
    //CGFloat borderW = 10;
    //2.加载图片
    //UIImage *oriImage = [UIImage imageNamed:@"阿狸头像"];
    //3.开启位图上下文(大小 原始图片的宽高度+ 2 *边框宽度)
    CGSize size = CGSizeMake(oriImage.size.width + 2 * borderW, oriImage.size.height + 2 * borderW);
    UIGraphicsBeginImageContext(size);
    //4.绘制边框(大圆)
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)];
    [boderColor set];
    [path fill];
    //5.绘制小圆(把小圆设置成裁剪区域)
    UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderW, borderW, oriImage.size.width, oriImage.size.height)];
    [clipPath addClip];
    //6.把图片绘制到上下文当中
    [oriImage drawAtPoint:CGPointMake(borderW, borderW)];
    //7.从上下文当中生成图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //8.关闭上下文.
    UIGraphicsEndImageContext();

    return newImage;

}

+ (UIImage *)imageWithString:(NSString *)oriString image:(UIImage *)oriImage {
    // 生成一张图片
    // 1.创建位图上下文(size:开启多大的上下文,就会生成多大的图片)
    UIGraphicsBeginImageContext(oriImage.size);
    // 2.把图片绘制到上下文当中
    [oriImage drawAtPoint:CGPointZero];
    // 3.绘制水印
    NSString *str = oriString;


    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    dict[NSForegroundColorAttributeName] = [UIColor redColor];

    [str drawAtPoint:CGPointZero withAttributes:dict];
    //4.从上下文当中生成一张图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //5.关闭位图上下文
    UIGraphicsEndImageContext();

    return newImage;
}

+ (UIImage *)imageWithOriImage:(UIImage *)oriImage {
    //生成一张圆形图片
    //1.开启一个位图上下文
    UIGraphicsBeginImageContext(oriImage.size);
    //2.设置一个裁剪区域(圆形)
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, oriImage.size.width, oriImage.size.height)];
    //把路径设置成裁剪区域(超过裁剪区域以外的内容会自动被裁剪掉)
    [path addClip];

    //3.把图片绘制到上下文当中
    [oriImage drawAtPoint:CGPointZero];
    //4.从上下文当中生成一张图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //5.关闭上下文
    UIGraphicsEndImageContext();

    return newImage;
}

@end


// ViewController.h
#import 

@interface ViewController : UIViewController


@end

// ViewController.m
#import "ViewController.h"
#import "UIImage+image.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imageV;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 已经裁剪好的图片
//    self.imageV.image = [UIImage imageWithOriImage:[UIImage imageNamed:@"阿狸头像"]];
    // 已经裁剪好的带有边框的图片
//    self.imageV.image = [UIImage imageWithBorder:10 color:[UIColor greenColor] image:[UIImage imageNamed:@"阿狸头像"]];
    // 图片加文字
    self.imageV.image = [UIImage imageWithString:@"思行先生" image:[UIImage imageNamed:@"小黄人"]];

}

@end

你可能感兴趣的:(图片水印和裁剪)