gpt4 book ai didi

objective-c - Objectivc C 保留层的上下文

转载 作者:搜寻专家 更新时间:2023-10-30 20:25:20 26 4
gpt4 key购买 nike

我正在使用 -(void)drawInContext:(CGContextRef)ctxCALayer 绘制一个形状,如下所示:

UIGraphicsPushContext(ctx);
CGContextBeginPath(ctx);
... draw path
CGContextClosePath(ctx);

CGContextClip(ctx);
CGContextSaveGState(ctx);
[self.image drawAtPoint:CGPointZero];
CGContextDrawPath(ctx, kCGPathFillStroke); // Paint the context
CGContextRestoreGState(ctx);
UIGraphicsPopContext();

我有多个带有此 CALayer 的 UIView。当特定事件发生时,我需要获取 CALayer 上下文绘图并将其应用于另一个 UIView 中的另一个 CALayer,同时保留当前上下文绘图。我尝试从上下文中缓存 UImage 然后重新应用它但无济于事。

self.cachedImage = UIGraphicsGetImageFromCurrentImageContext();

我尝试重新应用它:

self.contents = (id)self.cachedImage.CGImage;

[self.cachedImage drawAtPoint:CGPointZero];

我对此很陌生,需要一些指导以了解如何保留或添加这些 CALayer 的上下文绘图

编辑

我也在 UIView 而不是 CALayer 上尝试过。这是我的:

-(void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.cachedImage.image drawAtPoint:CGPointZero blendMode:kCGBlendModeNormal alpha:1.0];
CGContextBeginPath(ctx);
... draw path ...
CGContextClosePath(ctx);
CGContextClip(ctx);
[self.image drawAtPoint:CGPointZero blendMode:kCGBlendModeNormal alpha:1.0];
CGContextDrawPath(ctx, kCGPathFillStroke);

UIGraphicsBeginImageContext(self.bounds.size);
self.cachedImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.cachedImage setAlpha:1.0];
UIGraphicsEndImageContext();
}

这仍然清除了过去的上下文绘制。如果我使用 NSLog self.cachedImage.image 它肯定存在。

我的一部分想知道它是否与此有关:CGContextClip()

最佳答案

你真的不应该在 -drawRect: 中设置任何东西,只能绘制。您尝试做的事情很可能应该在其他地方完成。据我了解,您正在尝试将两个 View 的绘图“合并”为一个图像?如果是这样,请在 -drawRect: 之外单独创建图像上下文、绘制和保存这些图像,如 Tommy状态,您应该只绘制 View 的状态,而不是更改对象的任何内容。

看看UIGraphicsBeginImageContext一个好的起点(附注:这也可以在后台线程上运行!)。

关于objective-c - Objectivc C 保留层的上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17091478/

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