码迷,mamicode.com
首页 > 移动开发 > 详细

iOS UIImage 图片水印,图片裁剪,屏幕截图,背景平铺

时间:2015-01-23 12:34:38      阅读:337      评论:0      收藏:0      [点我收藏+]

标签:

图片水印功能

 1 #import "UIImage+ZR.h"
 2 
 3 @implementation UIImage (ZR)
 4 + (instancetype)waterImageWithBg:(NSString *)bg logo:(NSString *)logo
 5 {
 6     UIImage *bgImage = [UIImage imageNamed:bg];
 7     
 8     // 1.创建一个基于位图的上下文(开启一个基于位图的上下文)
 9     UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0);
10     
11     // 2.画背景
12     [bgImage drawInRect:CGRectMake(0, 0, bgImage.size.width, bgImage.size.height)];
13     
14     // 3.画右下角的水印
15     UIImage *waterImage = [UIImage imageNamed:logo];
16     CGFloat scale = 0.2;
17     CGFloat margin = 5;
18     CGFloat waterW = waterImage.size.width * scale;
19     CGFloat waterH = waterImage.size.height * scale;
20     CGFloat waterX = bgImage.size.width - waterW - margin;
21     CGFloat waterY = bgImage.size.height - waterH - margin;
22     [waterImage drawInRect:CGRectMake(waterX, waterY, waterW, waterH)];
23     
24     // 4.从上下文中取得制作完毕的UIImage对象
25     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
26     
27     // 5.结束上下文
28     UIGraphicsEndImageContext();
29     
30     return newImage;
31 }
32 @end

图片裁剪
 1 #import "UIImage+ZR.h"
 2 
 3 @implementation UIImage (ZR)
 4 + (instancetype)circleImageWithName:(NSString *)name borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor
 5 {
 6     // 1.加载原图
 7     UIImage *oldImage = [UIImage imageNamed:name];
 8     
 9     // 2.开启上下文
10     CGFloat imageW = oldImage.size.width + 2 * borderWidth;
11     CGFloat imageH = oldImage.size.height + 2 * borderWidth;
12     CGSize imageSize = CGSizeMake(imageW, imageH);
13     UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
14     
15     // 3.取得当前的上下文
16     CGContextRef ctx = UIGraphicsGetCurrentContext();
17     
18     // 4.画边框(大圆)
19     [borderColor set];
20     CGFloat bigRadius = imageW * 0.5; // 大圆半径
21     CGFloat centerX = bigRadius; // 圆心
22     CGFloat centerY = bigRadius;
23     CGContextAddArc(ctx, centerX, centerY, bigRadius, 0, M_PI * 2, 0);
24     CGContextFillPath(ctx); // 画圆
25     
26     // 5.小圆
27     CGFloat smallRadius = bigRadius - borderWidth;
28     CGContextAddArc(ctx, centerX, centerY, smallRadius, 0, M_PI * 2, 0);
29     // 裁剪(后面画的东西才会受裁剪的影响)
30     CGContextClip(ctx);
31     
32     // 6.画图
33     [oldImage drawInRect:CGRectMake(borderWidth, borderWidth, oldImage.size.width, oldImage.size.height)];
34     
35     // 7.取图
36     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
37     
38     // 8.结束上下文
39     UIGraphicsEndImageContext();
40     
41     return newImage;
42 }
43 @end

 

屏幕截图

 1 #import "UIImage+ZR.h"
 2 
 3 @implementation UIImage (ZR)
 4 + (instancetype)captureWithView:(UIView *)view
 5 {
 6     // 1.开启上下文
 7     UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0);
 8     
 9     // 2.将控制器view的layer渲染到上下文
10     [view.layer renderInContext:UIGraphicsGetCurrentContext()];
11     
12     // 3.取出图片
13     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
14     
15     // 4.结束上下文
16     UIGraphicsEndImageContext();
17     
18     return newImage;
19 }
20 @end

 

背景拉伸

 1 - (void)imageBg
 2 {
 3     UIImage *oldImage = [UIImage imageNamed:@"me"];
 4     
 5     UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0);
 6     [oldImage drawInRect:self.view.bounds];
 7     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
 8     UIGraphicsEndImageContext();
 9     
10     self.view.backgroundColor = [UIColor colorWithPatternImage:newImage];
11 }

 

背景平铺

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     
 5 //    self.view.backgroundColor = [UIColor redColor];
 6     
 7     // 1.创建一行背景图片
 8     CGFloat rowW = self.view.frame.size.width;
 9 //    CGFloat rowH = 40;
10     CGFloat rowH = 30;
11     UIGraphicsBeginImageContextWithOptions(CGSizeMake(rowW, rowH), NO, 0.0);
12     
13     CGContextRef ctx = UIGraphicsGetCurrentContext();
14     // 画矩形框
15     [[UIColor redColor] set];
16     CGContextAddRect(ctx, CGRectMake(0, 0, rowW, rowH));
17     CGContextFillPath(ctx);
18     
19     // 2.画线
20     [[UIColor greenColor] set];
21     CGFloat lineWidth = 2;
22     CGContextSetLineWidth(ctx, lineWidth);
23     CGFloat dividerX = 0;
24     CGFloat dividerY = rowH - lineWidth;
25     CGContextMoveToPoint(ctx, dividerX, dividerY);
26     CGContextAddLineToPoint(ctx, rowW - dividerX, dividerY);
27     CGContextStrokePath(ctx);
28     
29     // 3.取图
30     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
31     
32     // 4.结束上下文
33     UIGraphicsEndImageContext();
34     
35     // 5.设置为背景
36     self.textView.backgroundColor = [UIColor colorWithPatternImage:newImage];
37 }

 

 

 

iOS UIImage 图片水印,图片裁剪,屏幕截图,背景平铺

标签:

原文地址:http://www.cnblogs.com/airy99/p/4243578.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!