答案就是:畫
性能高
可以給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)注“簡書作者”。