iOS-设置图片圆角

  • 常用设置:
//cornerRadius 设置为self.iconImage图片宽度的一半(圆形图片)   
 self.iconImage.layer.cornerRadius = 20;
 self.iconImage.layer.masksToBounds = YES;
  • 适用 xib 或者 storyboard
iOS-设置图片圆角_第1张图片
Paste_Image.png
  • 绘图做法

使用图层过量会有卡顿现象, 特别是弄圆角或者阴影会很卡, 如果设置图片圆角一般用绘图来做

//写个UIImage分类
#import 

@interface UIImage (myExtension)

+ (UIImage *)cutCircleImage:(NSString *)imageName;

@end

实现

#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.jianshu.com/p/6cf66c95fa4d

iOS图片设置圆角性能问题:
http://www.jianshu.com/p/34189f62bfd8

你可能感兴趣的:(iOS-设置图片圆角)