ios圆形图片

方式一:

_imageView.image = [UIImage imageNamed:@"pic"];
_imageView.layer.cornerRadius = _imageView.frame.size.width/2;
_imageView.layer.masksToBounds = YES;

这种方式不建议使用,因为使用图层过量,特别是弄圆角或者阴影会很卡,设置图片圆角一般采用绘图来做。如下

方式二:
定义一个UIImage的分类,将方法写进该分类:

#import 
@interface UIImage (shape)
- (UIImage *)cutCircleImage;
@end
#import "UIImage+shape.h"

@implementation UIImage (shape)

/** 设置圆形图片(放到分类中使用) */
- (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 *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultImage;
}

调用该方法:

_imageView.image = [[UIImage imageNamed:@"pic"] cutCircleImage];

你可能感兴趣的:(ios圆形图片)