//創(chuàng)建UIImageView
UIImageView *imageV = [UIImageView alloc] initWithFrame:CGMake(0,0,100,100)];
[view addSubView:imageV];
如果需要處理的圖形比較多,最好利用第二種方法里面的2個(gè)方法去實(shí)現(xiàn)
1.第一種方法:設(shè)置UIImageView的layer的圓角,然后裁剪 (這種方法比較損耗性能)
imageV.layer.cornerRadius = imageV.Frame.size.Width;
imageV.layer.masksToBounds = YES;
2.利用貝塞爾將imageV整個(gè)類容畫在一個(gè)圖形上下文中,在圖形上下文中畫成圓形
給UIImageView空充一個(gè)分類,分別實(shí)現(xiàn)2個(gè)方法
-
(void)drawRoundImageToImageView
{
//設(shè)置圖片上下文的大小
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 1.0);[[UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:self.bounds.size.width]addClip];
[self drawRect:self.bounds];
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
} -
(void)drawShapeLayerRoundRect
{
UIBezierPath *maskPaht = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:self.bounds.size];CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
shapeLayer.frame = self.bounds;
shapeLayer.path = maskPaht.CGPath;
self.layer.mask = shapeLayer;
}