我知道的實現方法有三種。
1、通過image mask來操作,需要添加mask目標圖片。
2、通過imageview的layer來操作
如下代碼
- UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"oiuyfdsa.png"]];
- imageView.frame = CGRectMake(20.f, 20.f, 100.f, 100.f);
- imageView.layer.masksToBounds = YES;
- imageView.layer.cornerRadius = 50;
a.這種方法需要添加QuarztCore框架才能操作
b.cornerradus的確定問題
3、能過代碼對畫布裁剪成圓形–》然後再將原始圖像畫出來–》
- -(UIImage*) circleImage:(UIImage*) image withParam:(CGFloat) inset {
- UIGraphicsBeginImageContext(image.size);
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextSetLineWidth(context, 2);
- CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
- CGRect rect = CGRectMake(inset, inset, image.size.width - inset * 2.0f, image.size.height - inset * 2.0f);
- CGContextAddEllipseInRect(context, rect);
- CGContextClip(context);
- [image drawInRect:rect];
- CGContextAddEllipseInRect(context, rect);
- CGContextStrokePath(context);
- UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return newimg;
- }
上面代碼注意 如果裁剪後沒有使用 CGContextAddEllipseInRect(context, rect);
CGContextStrokePath(context); 這條代碼 就會引起背景爲白色時看不出來任務效果。
這裏是橢圓操作