切圓角的最優(yōu)方法

答案就是:畫

性能高
可以給UIImage
添加一個分類UIImage+Extension

分類中增加一個返回圓形圖片的方法,擴(kuò)展性強(qiáng)

#import <UIKit/UIKit.h>
#import "UIImage+Extension.h"
@interface UIImage (Extension)
- (UIImage *)circleImage;
@end

@implementation UIImage (Extension)
- (UIImage *)circleImage 
{
 // 開始圖形上下文  
  UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0); 
// 獲得圖形上下文 
  CGContextRef ctx = UIGraphicsGetCurrentContext(); 
// 設(shè)置一個范圍 
  CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height); 
// 根據(jù)一個rect創(chuàng)建一個橢圓 
  CGContextAddEllipseInRect(ctx, rect); 
// 裁剪
   CGContextClip(ctx);
// 將原照片畫到圖形上下文 
  [self drawInRect:rect];
// 從上下文上獲取剪裁后的照片
   UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
// 關(guān)閉上下文 
  UIGraphicsEndImageContext();

   return newImage;
}
具體使用:
// 獲得的就是一個圓形的圖片
UIImage *placeHolder = [[UIImage imageNamed:@"defaultUserIcon"] circleImage];

文/YotrolZ(簡書作者)原文鏈接:http://www.lxweimin.com/p/068d6f493547#著作權(quán)歸作者所有,轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),并標(biāo)注“簡書作者”。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容