提高 CALayer 过滤器的性能

发布于 2024-10-13 07:02:41 字数 1345 浏览 9 评论 0原文

我正在开发一个 Cocoa 全屏应用程序。我正在使用 1 个 NSView,它有 1 个具有多个子层的 CALayer。现在进行测试 - 我正在使用任何击键在屏幕上添加点(20 x 20)。这只是为了测试绘制点。我的问题是,我在点图层上使用了滤镜 - 具体来说,我使用的是 CIDiscBlur - 一旦我达到大约 30 个点 - 点的绘制速度就会显着减慢。按键和点出现之间可能有 1 - 1.5 秒的延迟。我注意到,如果我删除在图层上设置 CIDisBlur 过滤器 - 速度不会减慢。

在绘制这么多子图层时,我应该使用哪些最佳实践或技巧?任何帮助都会很棒。

    CIFilter *blurFilter = [CIFilter filterWithName:@"CIDiscBlur"];
    [blurFilter setDefaults];
    [blurFilter setValue:(id)[NSNumber numberWithFloat:15.0] forKey:@"inputRadius"];

    dotFilters = [[NSArray arrayWithObjects:(id)blurFilter, nil] retain];

    CGColorRef purpleColor = CGColorCreateGenericRGB(0.604, 0.247, 0.463, 1.0);

    CALayer *dot = [[CALayer layer] retain];
    dot.backgroundColor = purpleColor;
    dot.cornerRadius = 15.0f;
    dot.filters = dotFilters;

    NSRect screenRect = [[self.window screen] frame];

    //  10 point border around the screen

    CGFloat width = screenRect.size.width - 20;
    CGFloat height = screenRect.size.height - 20;

    #define ARC4RANDOM_MAX      0x100000000
    width = ((CGFloat)arc4random() / ARC4RANDOM_MAX) * width + 10;
    height = ((CGFloat)arc4random() / ARC4RANDOM_MAX) * height + 10;

    dot.frame = CGRectMake(width, height, 20,20);//30, 30);


    [dot addSublayer:dotsLayer];

我还尝试使用 masksToBounds = YES 来看看是否有帮助 - 但没有运气。

I'm working on a Cocoa fullscreen application. I am using 1 NSView that has 1 CALayer that has multiple sublayers. Right now for testing - I am using any keystrokes to add dots (20 x 20 ) to the screen. This is just for testing of drawing the dots. My issue is that I am using a filter on my dot layers - specifically I am using CIDiscBlur - and once I reach about 30 dots - the drawing of the dots significantly slows down. There can be a 1 - 1.5 second delay between the key press and the appearance of the dot. I have noticed that if I remove setting the CIDisBlur filter on the layers - that there is no slow down.

Are there any best practices or tips I should be using when drawing this many sublayers? Any help would be great.

    CIFilter *blurFilter = [CIFilter filterWithName:@"CIDiscBlur"];
    [blurFilter setDefaults];
    [blurFilter setValue:(id)[NSNumber numberWithFloat:15.0] forKey:@"inputRadius"];

    dotFilters = [[NSArray arrayWithObjects:(id)blurFilter, nil] retain];

    CGColorRef purpleColor = CGColorCreateGenericRGB(0.604, 0.247, 0.463, 1.0);

    CALayer *dot = [[CALayer layer] retain];
    dot.backgroundColor = purpleColor;
    dot.cornerRadius = 15.0f;
    dot.filters = dotFilters;

    NSRect screenRect = [[self.window screen] frame];

    //  10 point border around the screen

    CGFloat width = screenRect.size.width - 20;
    CGFloat height = screenRect.size.height - 20;

    #define ARC4RANDOM_MAX      0x100000000
    width = ((CGFloat)arc4random() / ARC4RANDOM_MAX) * width + 10;
    height = ((CGFloat)arc4random() / ARC4RANDOM_MAX) * height + 10;

    dot.frame = CGRectMake(width, height, 20,20);//30, 30);


    [dot addSublayer:dotsLayer];

I also tried using masksToBounds = YES to see if that helped - but no luck.

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

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

发布评论

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

评论(1

各空 2024-10-20 07:02:41

通过不使用圆角半径来制作圆形图层,您可能可以获得性能提升。虽然在静态上下文中制作圆形图层是一个不错的小捷径,但当您制作动画时,它会显着降低性能。您最好指定一个 CAShapeLayer 的圆形路径,或者下拉到 Core Graphics 并在 drawInContext 调用中绘制一个圆圈。要测试我是否正确,只需注释掉您设置圆角半径的调用并应用您的过滤器即可。看看这是否会加快速度。如果没有,那么我不确定发生了什么。这可能意味着您必须找到一种不同的方法来在没有滤镜的情况下获得效果。如果您的点总是具有相同的外观,那么您可能可以通过使用图像来“作弊”。

此致。

You can probably get a performance gain by not using corner radius to make your round layers. While it's a nice little shortcut to just make a round layer in a static context, when you're animating, it will degrade performance significantly. You'd be better off specifying a circular path to a CAShapeLayer or dropping down to Core Graphics and just drawing a circle in the drawInContext call. To test if I'm right, just comment out your call to set the corner radius and apply your filter. See if that speeds things up. If not, then I'm not sure what's up. It may mean you'll have to find a different way to get your effect without a filter. If you'll always have the same look for your dots, you'll can probably "cheat" by using an image.

Best regards.

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