自定义View03——圆形头像
一、最主要的方法
// 指定上下文中可以显示内容的范围
// 注意,指定范围(也就是指点剪切的方法一定要在绘制范围之前调用)
// 先绘制我们要指定的局域,然后再在指定的区域内画图
CGContextClip(ctx);
二、代码
//
// WMCircleHeadView.m
// 03_UIView45_圆形头像
//
// Created by 杞文明 on 2016/04/14 08:00:28 星期四
// Copyright © 2016年 杞文明. All rights reserved.
//
#import "WMCircleHeadView.h"
@implementation WMCircleHeadView
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
//1. 获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2. 绘制
CGFloat width = rect.size.width;
CGFloat height = rect.size.height;
//画一个圆
CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, width, height));
// 指定上下文中可以显示内容的范围
// 注意,指定范围(也就是指点剪切的方法一定要在绘制范围之前调用)
// 先绘制我们要指定的局域,然后再在指定的区域内画图
CGContextClip(ctx);
//画图片
UIImage *image = [UIImage imageNamed:@"me"];
[image drawInRect:CGRectMake(0, 0, width, height)];
//3. 渲染
CGContextFillPath(ctx);
}
@end