gpt4 book ai didi

ios - 如何缓存 CGContextRef

转载 作者:可可西里 更新时间:2023-11-01 05:57:08 25 4
gpt4 key购买 nike

由于对我之前的结果不满意,我被要求创建一个徒手绘图 View ,该 View 在缩放时不会模糊。我能想到这是可能的唯一方法是使用 CATiledLayer,否则在缩放时画线会太慢。目前,我已将其设置为每次都会重绘每一行,但我想知道我是否可以缓存前几行的结果(不是像素,因为它们需要很好地缩放)在上下文或其他内容中。

我想到了 CGBitmapContext,但这是否意味着我需要在每次缩放后拆除并设置一个新的上下文?问题是在 Retina 显示器上,线条绘制速度太慢(在 iPad 2 上一般),尤其是在缩放时绘制。 App Store 中有一个名为 GoodNotes 的应用程序,它漂亮地演示了这是可能的,并且可以顺利完成,但我无法理解他们是如何做到的。到目前为止,这是我的代码(今天大部分时间的结果):

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

CGContextSetLineWidth(c, mLineWidth);
CGContextSetAllowsAntialiasing(c, true);
CGContextSetShouldAntialias(c, true);
CGContextSetLineCap(c, kCGLineCapRound);
CGContextSetLineJoin(c, kCGLineJoinRound);

//Protect the local variables against the multithreaded nature of CATiledLayer
[mLock lock];
NSArray *pathsCopy = [mStrokes copy];
for(UIBezierPath *path in pathsCopy) //**Would like to cache these**
{
CGContextAddPath(c, path.CGPath);
CGContextStrokePath(c);
}
if(mCurPath)
{
CGContextAddPath(c, mCurPath.CGPath);
CGContextStrokePath(c);
}

CGRect pathBounds = mCurPath.bounds;
if(pathBounds.size.width > 32 || pathBounds.size.height > 32)
{
[mStrokes addObject:mCurPath];
mCurPath = [[UIBezierPath alloc] init];
}
[mLock unlock];
}

分析显示目前 HitTest 门的函数是 GCSFillDRAM8by1

最佳答案

首先,由于路径描边是最昂贵的操作,您不应该锁定它,因为这会阻止您在不同的核心上同时绘制图 block 。

其次,我认为您可以通过在上下文中添加所有路径并一起描边来避免多次调用 CGContextStrokePath

[mLock lock]; 
for ( UIBezierPath *path in mStrokes ) {
CGContextAddPath(c, path.CGPath);
}
if ( mCurPath ) {
CGContextAddPath(c, mCurPath.CGPath);
}
CGRect pathBounds = mCurPath.bounds;
if ( pathBounds.size.width > 32 || pathBounds.size.height > 32 )
{
[mStrokes addObject:mCurPath];
mCurPath = [[UIBezierPath alloc] init];
}
[mLock unlock];
CGContextStrokePath(c);

CGContextRef 只是一个在其中进行绘图操作的 Canvas 。您不能缓存它,但您可以创建一个 CGImageRef,其中包含路径的平面位图图像并重新使用该图像。这对缩放没有帮助(因为当细节级别发生变化时您需要重新创建图像),但在用户绘制很长的路径时可能有助于提高性能。

关于该主题有一个非常有趣的 WWDC 2012 session 视频:Optimizing 2D Graphics and Animation Performance .

关于ios - 如何缓存 CGContextRef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11859901/

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