Reputation: 667
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
Reputation: 104708
Option 0 - Make sure you draw only when you need to draw, and only what you need to draw.
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:
Of course, this only makes sense when the composite varies at a frequency lower than it must be drawn.
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).
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
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