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

vla*_*r z 7 drawing image objective-c uiimage ios

这正是它所需要的,除了它需要大约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();
}
Run Code Online (Sandbox Code Playgroud)

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

有没有办法更快地做到这一点?虽然仍然可以使用kCGBlendModeScreen和alpha?

jus*_*tin 8

您可以:

  • 获得UIImages的CGImages
  • 然后将它们绘制到CGBitmapContext
  • 从中产生一幅图像

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

其他考虑:

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