Umgre
Umgre

Reputation: 667

drawInRect performance too slow

What I'm doing is merge 2 images into single image.

Here is the code.

UIGraphicsBeginImageContext(CGSizeMake(1024, 768));

[image1 drawInRect:CGRectMake(0, 0, 512, 768)];
[image2 drawInRect:CGRectMake(512, 0, 512, 768)];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

But drawInRect seems too slow.

Is there faster way to merge images?

Upvotes: 1

Views: 3452

Answers (2)

justin
justin

Reputation: 104708

Option 0 - Make sure you draw only when you need to draw, and only what you need to draw.


Option 1

If you know the destination size (perhaps self.frame.size?), you can create one image from the two source images (flatten) at the destination size and avoid interpolation. So that could:

  1. Reduce the memory you need
  2. Reduce the number of images you must draw
  3. Avoid interpolation (High CPU if you want it to look good)
  4. Look better - the composite can use High Quality interpolation.

Of course, this only makes sense when the composite varies at a frequency lower than it must be drawn.

Option 2

Even if you want two images and you know their sizes will not change, just resize them to the size they must be drawn at (well, monitor your memory usage if you are enlarging them).

Option 3

If that's not an option, you could alter the CGContext's state, and reduce interpolation quality. If you're used to similar CALayer transformations, you would probably be satisfied with low quality or no interpolation.

Upvotes: 3

Catfish_Man
Catfish_Man

Reputation: 41831

One thing you could do is not implement -drawRect: at all, but instead have two CALayers with your two images as their contents, then put one in front of the other. Not sure that would be faster, but I think it's likely (since CA could then handle the drawing asynchronously, on the GPU).

Upvotes: 1

Related Questions