UIImage drawInRect:非常慢;有没有更快的方法?

发布于 2024-12-12 04:17:43 字数 634 浏览 0 评论 0原文

这正是它所需要的,除了它需要大约 400 毫秒,这太多了 350 毫秒:

 - (void) updateCompositeImage { //blends together the background and the sprites
    UIGraphicsBeginImageContext(CGSizeMake(480, 320));

    [bgImageView.image drawInRect:CGRectMake(0, 0, 480, 320)];

    for (int i=0;i<numSprites;i++) {
        [spriteImage[spriteType[i]] drawInRect:spriteRect[i] blendMode:kCGBlendModeScreen alpha:spriteAlpha[i]];
    }

    compositeImageView.image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
}

图像相当小,而且只有三个(for 循环只迭代两次)

有什么方法可以做这更快?同时仍然能够使用 kCGBlendModeScreen 和 alpha?

This does exactly what it needs to, except that it takes about 400 milliseconds, which is 350 milliseconds too much:

 - (void) updateCompositeImage { //blends together the background and the sprites
    UIGraphicsBeginImageContext(CGSizeMake(480, 320));

    [bgImageView.image drawInRect:CGRectMake(0, 0, 480, 320)];

    for (int i=0;i<numSprites;i++) {
        [spriteImage[spriteType[i]] drawInRect:spriteRect[i] blendMode:kCGBlendModeScreen alpha:spriteAlpha[i]];
    }

    compositeImageView.image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
}

The images are fairly small, and there are only three of them (the for loop only iterates twice)

Is there any way of doing this faster? While still being able to use kCGBlendModeScreen and alpha?

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

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

发布评论

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

评论(2

乖不如嘢 2024-12-19 04:17:43

您可以:

  • 获取 UIImages 的 CGImages
  • ,然后将它们绘制到 CGBitmapContext 中,
  • 生成图像

使用 CoreGraphics 本身 可能会更快。另一个好处是您可以在后台线程上执行渲染。还要考虑如何使用 Instruments 优化该循环和配置文件。

其他考虑因素:

  • 可以降低插值质量吗?
  • 源图像是否以任何方式调整大小(如果调整它们的大小会有所帮助)

you can:

  • get the UIImages' CGImages
  • then draw them to a CGBitmapContext
  • produce an image from that

using CoreGraphics in itself may be faster. the other bonus is that you can perform the rendering on a background thread. also consider how you can optimize that loop and profile using Instruments.

other considerations:

  • can you reduce the interpolation quality?
  • are the source images resized in any way (it can help if you resize them)
救赎№ 2024-12-19 04:17:43

绘制矩形很慢。时期。即使在小图像中,效率也非常低。

如果您要进行大量重复绘制,请看看 CGLayer,它旨在促进相同位的重复渲染

drawInRect is slow. Period. Even in small images it's grossly inefficient.

If you are doing a lot of repeat drawing, then have a look at CGLayer, which is designed to facilitate repeat-rendering of the same bits

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