如果与 CGContextSetShadowWithColor 一起使用,UIBezierPath 性能会降低

发布于 2024-12-25 21:28:42 字数 695 浏览 0 评论 0原文

我正在开发一个应用程序,用户可以用手指触摸画线或简单地喷涂颜色。 我正在使用 UIBezierPath 使用以下代码在 drawRect 方法中绘制手指触摸的路径(颜色)。

CGContextRef 上下文 = UIGraphicsGetCurrentContext();

for (BezierPath *path in paths)
{
    path.path.lineWidth = [DataController dataController].apertureRadius * 2;
    path.path.lineJoinStyle = kCGLineJoinRound;
    path.path.lineCapStyle = kCGLineCapRound;
    ///
    [path.color set];
    CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 20, [path.color CGColor]);//Problem
    //

    [path.path stroke];
}

该代码在模拟器上运行完美,但在设备上喷雾速度非常慢。 问题仅在于方法调用 CGContextSetShadowWithColor,如果我评论这条线性能是最好的,没有任何问题。

请告诉我为什么会这样以及我应该做什么。 这条线是必要的,因为我想用模糊阴影显示类似喷雾的效果。

I am developing an app in which user draws lines with finger touch or simply sprays colors.
I am using UIBezierPath to paint the path (colors) on finger touch in drawRect method with following code.

CGContextRef context = UIGraphicsGetCurrentContext();

for (BezierPath *path in paths)
{
    path.path.lineWidth = [DataController dataController].apertureRadius * 2;
    path.path.lineJoinStyle = kCGLineJoinRound;
    path.path.lineCapStyle = kCGLineCapRound;
    ///
    [path.color set];
    CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 20, [path.color CGColor]);//Problem
    //

    [path.path stroke];
}

This code works perfect on simulator but on Device the spray is really slow.
and the problem is only with method call CGContextSetShadowWithColor, if I comment this line performance is best with no problem at all.

Please suggest me why this is so and what should I do.
This line is necessary as I want to show the spray like effect with Blurry shadow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

死开点丶别碍眼 2025-01-01 21:28:42

绘制阴影可能会很慢。系统必须绘制到离屏缓冲区的路径,计算离屏缓冲区的 Alpha 通道到另一个离屏缓冲区的高斯模糊,然后将两个离屏缓冲区合成到原始图形上下文。

我怀疑每次更新触摸时,您都会重新绘制触摸所遵循的整个路径。您需要在每一帧上尽可能少地绘制。

保留您自己的位图上下文(使用 CGBitmapContextCreate 或 UIGraphicsBeginImageContextWithOptions 创建)。借鉴这个私人背景。当您收到触摸移动事件时,仅将笔划从触摸的旧位置绘制到新位置。然后从位图上下文中获取图像(使用 CGBitmapContextCreateImage 或 UIGraphicsGetImageFromCurrentImageContext )并将其设置为视图的图像或图层的内容。

Drawing a shadow can be slow. The system has to draw your path to an off-screen buffer, compute a Gaussian blur of the off-screen buffer's alpha channel to another off-screen buffer, then composite the two off-screen buffers into the original graphics context.

I suspect that every time a touch is updated, you're redrawing the entire path that the touch has followed. You need to draw as little as possible on each frame.

Keep your own bitmap context (created with either CGBitmapContextCreate or UIGraphicsBeginImageContextWithOptions). Draw to this private context. When you get a touch moved event, only draw the stroke from the touch's old location to the new location. Then get an image from the bitmap context (using either CGBitmapContextCreateImage or UIGraphicsGetImageFromCurrentImageContext) and set it as your view's image or your layer's contents.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文