ios Image裁剪成圆形 圆形头像


ios Image裁剪成圆形 圆形头像

1、通过image mask来操作,需要添加mask目标图片。

//通过image mask来操作,需要添加mask目标图片。

UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 300, 100, 100)];

UIGraphicsBeginImageContext(icon.bounds.size);

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextFillRect(ctx, CGRectMake(0, 0, icon.bounds.size.width, icon.bounds.size.height));

UIGraphicsEndImageContext();

UIImage *image = [UIImage imageNamed:@"main.jpg"];

UIImage *roundCorner = [UIImage imageNamed:@"corner_no_clear.png"];

icon.image = image;

CALayer* roundCornerLayer = [CALayer layer];

roundCornerLayer.frame = icon.bounds;

roundCornerLayer.contents = (id)[roundCorner CGImage];

[[icon layer] setMask:roundCornerLayer];

[self.view addSubview:icon];

2、通过imageview的layer来操作

//通过imageview的layer来操作

UIImage *main = [UIImage imageNamed:@"main.jpg"];

UIImage *corner_no_shadow = [UIImage imageNamed:@"corner_no_shadow.png"];

 

UIImageView *img0 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, 100, 100)];

img0.backgroundColor = [UIColor redColor];

img0.image = main;

img0.layer.masksToBounds = YES;

img0.layer.cornerRadius = 50;

[self.view addSubview:img0];

3、能过代码对画布裁剪成圆形–》然后再将原始图像画出来–》

//能过代码对画布裁剪成圆形–》然后再将原始图像画出来–》

UIImage *main = [UIImage imageNamed:@"main.jpg"];

UIImage *corner_no_shadow = [UIImage imageNamed:@"corner_no_shadow.png"];

 

UIImage *ii = [main circleImage:main withParam:0];

 

UIImageView *img1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 150, 100, 100)];

img1.image = ii;

[self.view addSubview:img1];

 

你可能感兴趣的:(image)