- 常用設置:
//cornerRadius 設置為self.iconImage圖片寬度的一半(圓形圖片)
self.iconImage.layer.cornerRadius = 20;
self.iconImage.layer.masksToBounds = YES;
- 適用 xib 或者 storyboard
Paste_Image.png
- 繪圖做法
使用圖層過量會有卡頓現(xiàn)象, 特別是弄圓角或者陰影會很卡, 如果設置圖片圓角一般用繪圖來做
//寫個UIImage分類
#import <UIKit/UIKit.h>
@interface UIImage (myExtension)
+ (UIImage *)cutCircleImage:(NSString *)imageName;
@end
實現(xiàn)
#import "UIImage+myExtension.h"
@implementation UIImage (myExtension)
- (UIImage *)cutCircleImage
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
// 獲取上下文
CGContextRef ctr = UIGraphicsGetCurrentContext();
// 設置圓形
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextAddEllipseInRect(ctr, rect);
// 裁剪
CGContextClip(ctr);
// 將圖片畫上去
[self drawInRect:rect];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+ (UIImage *)cutCircleImage:(NSString *)imageName
{
return [[self imageNamed:imageName] cutCircleImage];
}
@end
使用
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
imageV.image = [UIImage cutCircleImage:@"flower"];
self.iconImageView = imageV;
[self.view addSubview:self.iconImageView];
}
- 更多介紹
iOS-設置圖片圓角的幾種方法總結:
http://www.reibang.com/p/6cf66c95fa4d
iOS圖片設置圓角性能問題:
http://www.reibang.com/p/34189f62bfd8