`
dcj3sjt126com
  • 浏览: 1822794 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

ios Image裁剪成圆形 圆形头像

    博客分类:
  • IOS
 
阅读更多


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];

 

  • 大小: 38.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics