答案就是:畫
性能高
可以給UIImage
添加一個分類UIImage+Extension
分類中增加一個返回圓形圖片的方法,擴展性強
#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();
// 設置一個范圍
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
// 根據一個rect創建一個橢圓
CGContextAddEllipseInRect(ctx, rect);
// 裁剪
CGContextClip(ctx);
// 將原照片畫到圖形上下文
[self drawInRect:rect];
// 從上下文上獲取剪裁后的照片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// 關閉上下文
UIGraphicsEndImageContext();
return newImage;
}
具體使用:
// 獲得的就是一個圓形的圖片
UIImage *placeHolder = [[UIImage imageNamed:@"defaultUserIcon"] circleImage];
文/YotrolZ(簡書作者)原文鏈接:http://www.lxweimin.com/p/068d6f493547#著作權歸作者所有,轉載請聯系作者獲得授權,并標注“簡書作者”。