Search code examples
iosiphoneipaddrawingquartz-2d

CGBitmapInfo value performance on iOS


I am drawing pieces of a CGBitmapContext in the drawRect of a UIView. What are the best CGBitmapInfo enum values to ensure that performance is optimal? Right now I am using kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big, but I have also seen this stackoverflow question snippet which suggests an alternative:

Why does this code decompress a UIImage so much better than the naive approach?

// makes system don't need to do extra conversion when displayed.
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little

My CGBitmapContext is mutable, so the user can draw on it, add images to it, etc.


Solution

  • I ran tests with kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big vs kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little and the profiler did show me that there is a slightly different code path.

    kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little copies memory directly, while kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big goes through a conversion function.

    This made almost no impact in the total time it took to draw paths in my CGBitmapContext (even on an iPod 4) and then call setNeedsDisplayInRect: to show them on screen. Furthermore since the overall times are essentially identical, I'm sticking with kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big.

    EDIT I switched back to kCGBitmapByteOrder32Little in order to have better compatibility with other iOS frameworks (like AVFoundation) that use BGRA (little endian) byte orders since there was no impact on performance.