gpt4 book ai didi

iphone-给 CAShapeLayer *maskLayer 透明背景

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:42:36 25 4
gpt4 key购买 nike

我在 UIView 上有一个 UIImageView。在上面我使用 UIBezierPath 绘制了一个形状。现在我希望其他部分是透明的。我的代码是,

- (void)drawRect:(CGRect)rect
{
self.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.0];
imgV.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.0];
aPath = [UIBezierPath bezierPath];
CGContextRef aRef ;
aRef = UIGraphicsGetCurrentContext();
[[UIColor clearColor] setStroke];
[[UIColor clearColor] setFill];
aPath.lineWidth = 2;
aPath = [UIBezierPath new];
//path is calculated here using addLineToPoint addArcWithCentre methods
[aPath fill];
[aPath stroke];
[self setClippingPath:aPath :imgV];
[[[UIColor clearColor] colorWithAlphaComponent:0.0] setFill];
CGContextFillRect(aRef, rect);
CGContextFillPath(aRef);
self.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.0];
imgV.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.0];
}
- (void) setClippingPath:(UIBezierPath *)clippingPath : (UIImageView *)imgView;
{
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = imgView.frame;
maskLayer.path = [clippingPath CGPath];
maskLayer.backgroundColor = [[[UIColor clearColor] colorWithAlphaComponent:0.0] CGColor];
maskLayer.fillColor = [[UIColor whiteColor] CGColor];

[imgView removeFromSuperview];
imgView.layer.mask = maskLayer;

[self addSubview:imgView];

}

输出可以在下图中看到。图像 cur 周围的黑色部分应该是透明的 enter image description here请帮助我。

最佳答案

我认为您可能错过了最后一次调用,可能就在 addSubview 之前:

imgView.clipsToBounds = YES;

关于iphone-给 CAShapeLayer *maskLayer 透明背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16102770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com