加載需要顯示的圖片
UIImage *image = [UIImage imageNamed:@資源中圖片的名字];
初始化用到的變量
//圖片的寬寬高
CGFloat imageWH = image.size.width;
//邊界的寬度
CGFloat border = 1;
//背景區域的寬高
CGFloat ovalWH = imageWH + 2 * border;
開啟圖形上下文(尺寸為背景區域的尺寸
)
//設置圖形上下文的尺寸
CGSize size = CGSizeMake(ovalWH, ovalWH);
//開啟圖形上下文
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
繪制背景區域
//繪制封閉的圓形路徑
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, ovalWH, ovalWH)];
//設置填充顏色
[[UIColor blueColor] set];
//渲染所繪制的區域
[path fill];
繪制圖片區域
//繪制封閉的圓形區域
UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(border, border, imageWH, imageWH)];
//設置超出區域的內容剪掉(不顯示)
[clipPath addClip];
//見圖片繪制到圓形區域內,從點(border, border)開始
[image drawAtPoint:CGPointMake(border, border)];
從圖形上下文中取出繪制的圖片
UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();
將圖片顯示到imageView上
self.imageView.image = clipImage;
關閉圖形上下文
UIGraphicsEndImageContext();