【问题标题】:Attempting to mask a circle around an image not working试图掩盖图像周围的圆圈不起作用
【发布时间】:2015-07-23 19:21:30
【问题描述】:
我有一个图像,我试图掩盖一个圆圈,以便图像显示为圆形。这有点奏效,但圆圈在顶部和底部都有一个点。
profileImageView.layer.cornerRadius = profileImageView.frame.size.width/2;
profileImageView.layer.masksToBounds = YES;
这段代码应该画一个完美的圆吗?它似乎在一个地方画了一个圆圈,但在另外两个地方,它不能正常工作。
【问题讨论】:
标签:
ios
uiimageview
mask
cornerradius
【解决方案1】:
用CAShapeLayer 屏蔽图像视图的效果最好:
CGFloat radius = self.profileImageView.frame.size.width / 2.0;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(radius, radius) radius:radius startAngle:0 endAngle:M_PI * 2.0 clockwise:TRUE];
CAShapeLayer *layer = [CAShapeLayer layer];
layer.path = path.CGPath;
layer.lineWidth = 0;
self.profileImageView.layer.mask = layer;
【解决方案2】:
这段代码应该画一个完美的圆吗?
不一定。毕竟,这一层的宽度和高度可能不一样。即使它们是,除以 2 也可能无法得到一个完全适合整数点的半径,因为它们被映射到屏幕上的像素。
如果你想要的是一个圆形的蒙版,给这个层一个实际的一个实际的圆形蒙版会更好。滥用圆角半径只是懒惰(而且,正如您所发现的,它很容易出错)。