iOS:使用UIBezierPath屏蔽UIImage [英] iOS: Mask a UIImage using UIBezierPath
本文介绍了iOS:使用UIBezierPath屏蔽UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图掩盖图像,以便我只能给它两个圆角。使用我拥有的代码,它只是在图像上添加白色的蒙版,但似乎不适用它。我需要做些什么来掩盖图像边角?
I am trying to mask an image so that I can give it only two rounded corners. With the code that I have it just adds the mask in white over the image, but doesn't seem to apply it. What do I need to do different to mask the image corners?
CAShapeLayer *maskLayer = [CAShapeLayer layer];
UIBezierPath *roundedPath = [UIBezierPath bezierPathWithRoundedRect:maskLayer.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(16.f, 16.f)];
maskLayer.fillColor = [[UIColor whiteColor] CGColor];
maskLayer.backgroundColor = [[UIColor clearColor] CGColor];
maskLayer.path = [roundedPath CGPath];
// Add mask
self.imageView.layer.mask = maskLayer;
推荐答案
如上所述链接问题,您可能需要在应用其掩码之前从层次结构中删除该视图。
As mentioned in the above linked question, you probably need to remove the view from the heirarchy before applying its mask.
[self.imageView removeFromSuperview];
self.imageView.layer.mask = maskLayer;
[self.view addSubview:self.imageView];
此外,您的 maskLayer
没有边界
。您需要将其设置为您尝试屏蔽的视图的框架
(或边界
)。
Also, your maskLayer
has no bounds
. You need to set it to the frame
(or bounds
) of the view you're trying to mask.
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.imageView.frame;
这篇关于iOS:使用UIBezierPath屏蔽UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文