方式一:设置layer相关的属性
1 2 3 4 5
| UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"friendsRecommentIcon"]];
imageView.layer.cornerRadius = 5; [imageView.layer setMasksToBounds:YES];
|
方式二:画
- 性能高
- 可以给
UIImage
添加一个分类UIImage+Extension
- 分类中增加一个返回圆形图片的方法,扩展性强
1 2 3 4 5 6 7
| #import <UIKit/UIKit.h>
@interface UIImage (Extension)
- (UIImage *)circleImage;
@end
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| #import "UIImage+Extension.h"
@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); CGContextAddEllipseInRect(ctx, rect);
CGContextClip(ctx); [self drawInRect:rect]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }
|
具体使用:
1 2
| UIImage *placeHolder = [[UIImage imageNamed:@"defaultUserIcon"] circleImage];
|