gpt4 book ai didi

iphone - iOS:只绘制 UImage 的一部分

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

我正在尝试绘制 UIImage 的自定义部分(即:我想显示 UIImage 用户的部分触摸),我通过使用 layermask 属性得到了合理的结果。

在我的 UIView 上是这样的:

UIBezierPath *maskPath = [UIBezierPath bezierPath];
[maskPath setLineWidth:10.0];
[maskPath moveToPoint:CGPointMake(10.0, 10.0)];
[maskPath addLineToPoint:CGPointMake(100.0, 100.0)];
CAShapeLayer *shapeMaskLayer = [CAShapeLayer layer];
shapeMaskLayer.path = maskPath.CGPath;
[self.layer setMask:shapeMaskLayer];

然后,在 drawRect 上:

- (void)drawRect:(CGRect)rect
{
[img drawInRect:rect];
}

它有效。我只看到由 maskPath 定义的图像部分。

但是,看起来这并不是解决此问题的最佳方式。所以我的问题是:在 iOS SDK 中绘制图像的自定义部分(它可以是任何形状)的最佳方法是什么?

最佳答案

您可以尝试的一件事是简单地剪裁而不是创建一个额外的层。例如

- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();

UIBezierPath *maskPath = [UIBezierPath bezierPath];
[maskPath setLineWidth:10.0];
[maskPath moveToPoint:CGPointMake(10.0, 10.0)];
[maskPath addLineToPoint:CGPointMake(100.0, 100.0)];

CGContextAddPath(ctx, maskPath.CGPath);
CGContextClip(ctx)
[img drawInRect:rect];
}

关于iphone - iOS:只绘制 UImage 的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10214897/

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