简体   繁体   English

UIImage drawInRect:很慢; 有更快的方法吗?

[英]UIImage drawInRect: is very slow; is there a faster way?

This does exactly what it needs to, except that it takes about 400 milliseconds, which is 350 milliseconds too much: 这正是它所需要的,除了它需要大约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();
}

The images are fairly small, and there are only three of them (the for loop only iterates twice) 图像相当小,只有三个(for循环只迭代两次)

Is there any way of doing this faster? 有没有办法更快地做到这一点? While still being able to use kCGBlendModeScreen and alpha? 虽然仍然可以使用kCGBlendModeScreen和alpha?

you can: 您可以:

  • get the UIImages' CGImages 获得UIImages的CGImages
  • then draw them to a CGBitmapContext 然后将它们绘制到CGBitmapContext
  • produce an image from that 从中产生一幅图像

using CoreGraphics in itself may be faster. 使用CoreGraphics本身可能会更快。 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. 还要考虑如何使用Instruments优化该循环和配置文件。

other considerations: 其他考虑:

  • can you reduce the interpolation quality? 你能降低插值质量吗?
  • are the source images resized in any way (it can help if you resize them) 是以任何方式调整大小的源图像(如果你调整它们可以帮助)

drawInRect is slow. drawInRect很慢。 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 如果你正在做很多重复绘图,那么看看CGLayer,它旨在促进相同位的重复渲染

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM