Layer 图像绘制

一、示例一,截取一张圆形图片:

 1 /**
 2  * @method
 3  * @abstract 头像图片圆形样式
 4  * @discussion
 5  * @param
 6  * @result
 7  */
 8 - (UIImage *)getImgCircularWithImage:(UIImage *)aImage size:(CGSize)aSize
 9 {
10   // 创建一个基于位图的上下文(Context),相当于一个画布,以堆栈形式存储,并且将其设置为当前上下文(Context)。
11   UIGraphicsBeginImageContext(aSize);
12   // 获取当前上下文(Context)。
13   CGContextRef context = UIGraphicsGetCurrentContext();
14   // 设置当前上下文(Context)的位图的边框线宽度。
15   CGContextSetLineWidth(context, 2);
16   // 设置指定上下文(Context)笔画颜色。
17   CGContextSetStrokeColorWithColor(context, [UIColor clearColor].CGColor);
18   CGRect rect = CGRectMake(0, 0, aSize.width, aSize.height);
19   // 在指定上下文(Context)中,画一个椭圆。rect设置椭圆的大小。
20   CGContextAddEllipseInRect(context, rect);
21   // 在指定上下文(Context)中,画一条线。在API中还有画其它形状方法。
22   // CGPoint point = CGPointMake(1, 1);
23   // CGContextAddLines(context, point);
24   // 裁剪上下文(Context)多余部分。在API中,还提供了一些其它方法。
25   CGContextClip(context);
26   
27   // 绘制图形方法,绘制当前上下文的位图。
28   [aImage drawInRect:rect];
29   CGContextAddEllipseInRect(context, rect);
30   // 搭边或填充(绘制)指定路径的上下文(Context)。
31   CGContextStrokePath(context);
32   // 从当前上下文(Context)中,获取一个UIImage对象,当对象就是之前绘制的图片。
33   UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext();
34   // 位图绘制完成后,关闭位图的上下文(Context)
35   UIGraphicsEndImageContext();
36   return newImg;
37 }

示例二,生成一张纯色图片

 1 /**
 2  * @method
 3  * @abstract 通过颜色生成一张纯色图片
 4  * @discussion
 5  * @param (UIColor *) -- 图片颜色值
 6  * @param (CGSize) -- 图片尺寸
 7  * @result (UIImage *) 返回图片Image对象
 8  */
 9 - (UIImage *)generationImageSolidWithColor:(UIColor *)aColor size:(CGSize)aSize
10 {
11   UIImage *img = nil;
12   @try
13   {
14     CGRect rect = CGRectMake(0, 0, aSize.width, aSize.height);
15     // 创建一个位图上下文(Context),相当于一个画布,并且设置为当前上下文(Context)。
16     UIGraphicsBeginImageContext(rect.size);
17     // 获取当前上下文(Context)。
18     CGContextRef context = UIGraphicsGetCurrentContext();
19     // 为画布填充颜色。
20     CGContextSetFillColorWithColor(context, [aColor CGColor]);
21     // 设置画布填充颜色的范围。
22     CGContextFillRect(context, rect);
23     // 获取当前上下文(Context)的图片对象
24     img = UIGraphicsGetImageFromCurrentImageContext();
25     UIGraphicsEndImageContext();
26     
27   }
28   @catch (NSException *exception)
29   {
30     
31   }
32   @finally
33   {
34     
35   }
36   
37   return img;
38 }

示例三,将多张图片,合成一张图片

 1 /**
 2  * @method
 3  * @abstract 将两张图片,合成一张图片
 4  * @discussion
 5  * @param (UIImage *) -- 图片1
 6  * @param (UIImage *) -- 图片2
 7  * @result (UIImage *) 返回图片Image对象
 8  */
 9 - (UIImage *)synthesizeWithImage:(UIImage *)aImg1 withImage:(UIImage *)aImg2
10 {
11   UIImage *img = nil;
12   UIGraphicsBeginImageContext(aImg1.size);
13   [aImg1 drawInRect:CGRectMake(0, 0, aImg1.size.width, aImg1.size.height)];
14   [aImg2 drawInRect:CGRectMake(0, 0, aImg2.size.width, aImg2.size.height)];
15   img = UIGraphicsGetImageFromCurrentImageContext();
16   UIGraphicsEndImageContext();
17   return img;
18 }

PS: 绘图教程:http://www.cocoachina.com/industry/20140115/7703.html

原文地址:https://www.cnblogs.com/naray/p/4867235.html