给图片添加水印图片
+ (instancetype)waterImageWithBg:(NSString *)bg logo:(NSString *)logo
{
UIImage * bgImage = [UIImage imageNamed:bg];
UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0);
[bgImage drawInRect:CGRectMake(0, 0, bgImage.size.width, bgImage.size.height)];
UIImage * waterimage = [UIImage imageNamed:logo];
CGFloat scale = 0.2;
CGFloat waterW = waterimage.size.width * scale;
CGFloat waterH = waterimage.size.height * scale;
CGFloat waterX =bgImage.size.width-waterimage.size.width-10;
CGFloat waterY = bgImage.size.height - waterimage.size.height-10;
[waterimage drawInRect:CGRectMake(waterX, waterY, waterW, waterH)];
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
2.裁剪图片
+ (instancetype)circleImageWithName:(NSString *)name borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor
{
//1.加载原图
UIImage *oldImage = [UIImage imageNamed:name];
// 2.开启上下文
CGFloat imageW = oldImage.size.width + 2 * borderWidth;
CGFloat imageH = oldImage.size.height + 2 * borderWidth;
CGSize imageSize = CGSizeMake(imageW, imageH);
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
// 3.取得当前的上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 4.画边框(大圆)
[borderColor set];
CGFloat bigRadius = imageW * 0.5; // 大圆半径
CGFloat centerX = bigRadius; // 圆心
CGFloat centerY = bigRadius;
CGContextAddArc(ctx, centerX, centerY, bigRadius, 0, M_PI * 2, 0);
//
CGContextFillPath(ctx); // 画圆
//
// // 5.小圆
// CGFloat smallRadius = bigRadius - borderWidth;
// CGContextAddArc(ctx, centerX, centerY, smallRadius, 0, M_PI * 2, 0);
// /**
// *
// */
// // 裁剪(后面画的东西才会受裁剪的影响)
CGContextClip(ctx);
// // 6.画图
// [oldImage drawInRect:CGRectMake(borderWidth, borderWidth, oldImage.size.width, oldImage.size.height)];
// // 7.取图
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// // 8.结束上下文
// UIGraphicsEndImageContext();
return newImage;
}
截屏
+ (instancetype)captureWithView:(UIView *)view
{
// 1.开启上下文
UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0);
// 2.将控制器view的layer渲染到上下文
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
// 3.取出图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// 4.结束上下文
UIGraphicsEndImageContext();
return newImage;
}