我正在绘制一个形状,CALayer
如下-(void)drawInContext:(CGContextRef)ctx
所示:
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 的 UIViews。当发生特定事件时,我需要获取 CALayer 上下文绘图并将其应用于另一个 UIView 中的另一个 CALayer,同时保留当前上下文绘图。我尝试UImage
从上下文中缓存,然后重新应用它,但无济于事。
self.cachedImage = UIGraphicsGetImageFromCurrentImageContext();
我试过这个重新应用它:
self.contents = (id)self.cachedImage.CGImage;
或者
[self.cachedImage drawAtPoint:CGPointZero];
我对此很陌生,需要一些关于如何保存或添加到这些 CALayers 的上下文图中的指导
编辑
我也在 aUIView
而不是 a上尝试过这个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();
}
这仍然清除了过去的上下文绘图。如果我 NSLogself.cachedImage.image
它肯定存在。
我的一部分想知道它是否与此有关:CGContextClip()