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

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

- (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(); } 

Images are quite small, and there are only three of them (the for loop repeats only twice)

Is there any way to do this faster? Although you can still use kCGBlendModeScreen and alpha?

+6
source share
2 answers

You can:

  • get CGImages UIImages
  • then draw them in a CGBitmapContext
  • create image from this

Using CoreGraphics alone can be faster. Another bonus is that you can render on a background thread. Also consider how you can optimize this cycle and profile using tools.

other considerations:

  • Can you reduce the quality of the interpolation?
  • are source images resized in any way (this may help if you resize them)
+8
source

drawInRect is slow. Period. Even in small images, it is very inefficient.

If you do a lot of re-drawing, look at CGLayer, which is designed to make it easy to re-play the same bits

-1
source

Source: https://habr.com/ru/post/900174/


All Articles