第一種方法
是對圖層進(jìn)行操作
/** 要操作的圖片 */ @property (nonatomic,retain) UIImageView * imageView ;
直接對 圖片的 layer進(jìn)行操作
self.imageView.layer.cornerRadius = 10;// 圖片半徑
self.imageView.layer.masksToBounds = YES;// 可剪裁
注意:缺點惯悠, 要是同時加載的太多的原型頭像 會特別卡 !p睦铡3某摹轧抗!
第二種方法
1.開啟圖形上下文,圖形上下文 必須是透明的
2.在圖形上下文里面添加一個圓形,同時添加一個矩形框
3.然后根據(jù)圓形 秉氧,將矩形框剪裁成 圓形
4.然后將圖片畫上去
5.關(guān)閉上下文
UIGraphicsBeginImageContext( ) //不透明的, 不用
// 開啟上下文
// 透明的
UIGraphicsBeginImageContextWithOptions(當(dāng)前圖片的尺寸self.size, 是否透明 NO代表透明, 0.0);
// 獲取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 添加一個圓
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);//矩形框 就是圖片的大小
CGContextAddEllipseInRect(ctx, rect);
// 裁剪
CGContextClip(ctx);// 是根據(jù)
// 將圖片畫上去
[self drawInRect:rect];
// 獲取一張圖片
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
// 關(guān)閉上下文
UIGraphicsEndImageContext();