iOS

裁剪图片(圆角(Quartz2D))

2015-11-12  本文已影响257人  iOS_成才录
裁剪圆角图片.png

1、思路分析

2、 代码实现

  #import "DrawView.h"
                
                #define kBorderWith 10
                
                
                @implementation DrawView
                
                - (void)drawRect:(CGRect)rect {
                    // 获取图形上下文
                    CGContextRef ctx = UIGraphicsGetCurrentContext();
                    
                    /**
                     *  1、绘画大圆
                     */
                    CGRect bigRect = CGRectMake(rect.origin.x + kBorderWith, rect.origin.y+ kBorderWith, rect.size.width - kBorderWith*2, rect.size.height - kBorderWith*2);
                    
                    CGContextSetLineWidth(ctx, kBorderWith);
                    
                    CGContextAddEllipseInRect(ctx, bigRect);
                    
                    [[UIColor whiteColor] set];
                    
                    CGContextStrokePath(ctx);
                   
                    /**
                     *  2、绘画小圆,设置为裁剪区域
                     */
                    CGRect smallRect = CGRectMake(rect.origin.x + kBorderWith, rect.origin.y + kBorderWith, rect.size.width - 2 * kBorderWith, rect.size.height -2 * kBorderWith);
                    CGContextAddEllipseInRect(ctx, smallRect);
                   
                    CGContextClip(ctx);
                    
                    // 裁剪图片
                    UIImage *image = [UIImage imageNamed:@"阿狸头像"];
                    // 设置图片的裁剪区域为小圆
                    [image drawInRect:smallRect];
                    
                    // 将上下文的内容渲染到视图的layer图层上
                    CGContextStrokePath(ctx);
                }
                
                @end
                
                
                #define kBorderWith 10
                
                
                @implementation DrawView
                
                - (void)drawRect:(CGRect)rect {
                    // 获取图形上下文
                    CGContextRef ctx = UIGraphicsGetCurrentContext();
                    
                    /**
                     *  1、绘画大圆
                     */
                    CGRect bigRect = CGRectMake(rect.origin.x + kBorderWith, rect.origin.y+ kBorderWith, rect.size.width - kBorderWith*2, rect.size.height - kBorderWith*2);
                    
                    CGContextSetLineWidth(ctx, kBorderWith);
                    
                    CGContextAddEllipseInRect(ctx, bigRect);
                    
                    [[UIColor whiteColor] set];
                    
                    CGContextStrokePath(ctx);
                   
                    /**
                     *  2、绘画小圆,设置为裁剪区域
                     */
                    CGRect smallRect = CGRectMake(rect.origin.x + kBorderWith, rect.origin.y + kBorderWith, rect.size.width - 2 * kBorderWith, rect.size.height -2 * kBorderWith);
                    CGContextAddEllipseInRect(ctx, smallRect);
                   
                    CGContextClip(ctx);
                    
                    // 裁剪图片
                    UIImage *image = [UIImage imageNamed:@"阿狸头像"];
                    // 设置图片的裁剪区域为小圆
                    [image drawInRect:smallRect];
                    
                    // 将上下文的内容渲染到视图的layer图层上
                    CGContextStrokePath(ctx);
                }
                
                @end
上一篇下一篇

猜你喜欢

热点阅读