Drawing image with CoreGraphics on Retina iPad is slow
Asked Answered
E

1

10

In my iPad app, I am rendering to an offscreen bitmap, and then drawing the bitmap to the screen. (This is because I want to re-use existing bitmap rendering code.) On the iPad 2, this works like a charm, but on the new iPad with Retina display, drawing the bitmap is really slow, even though its resolution is still the same.

To draw the bitmap, we use the regular Quartz 2D functions: CGImageCreate with a data provider created by CGDataProviderCreateWithData, 32-bit RGBA format with kCGImageAlphaNoneSkipLast. In the UIView that displays the bitmap, in drawRect:, we use CGContextDrawImage to draw it to the context returned by UIGraphicsGetCurrentContext.

Note that I'm not even trying to draw at double resolution: for now I'm fine with the same resolution as I was using on the iPad 2. It looks like CoreGraphics is internally doubling the pixels, and then sending that to the GPU, even though the CGImage that I'm making should be fine for passing to the GPU directly. Any ideas?

Eyestalk answered 2/5, 2012 at 8:22 Comment(0)
I
15

It looks like CoreGraphics is internally doubling the pixels, and then sending that to the GPU,

Pretty much. More accurately (in spirit at least):

  1. UIKit makes a CGBitmapContext the size of your view's bounds, in device pixels
  2. It makes that context the current context
  3. You draw your CGImage into that context
  4. ... so CG has to rescale the source image, and touch all of the destination pixels
  5. After you're done drawing, UIKit makes a CGImage from the bitmap context
  6. and assigns it to the view's layer's contents.

the CGImage that I'm making should be fine for passing to the GPU directly.

If you want that to happen, you need to tell the system to do that, by cutting out some of the steps above.

(There is no link between UIKit, CoreAnimation, and CoreGraphics that provides a "fast path" like you are expecting.)

The easiest way would be to make a UIImageView, and set its image to a UIImage wrapping your CGImageRef.

Or, set your view.layer.contents to your CGImageRef. (And make sure to not override -drawRect:, not call -setNeedsDisplay, and make sure contentMode is not UIViewContentModeRedraw. Easier to just use UIImageView.)

Impeditive answered 3/5, 2012 at 3:20 Comment(4)
Thanks for the info, that will probably help a lot. The view is animated, so it redraws often. I think that therefore using view.layer.contents is probably more efficient than assigning a new image to a UIImageView, right?Eyestalk
Tried it and the view.layer.contents method works brilliantly, thanks again!Eyestalk
AFAIK the two methods are nearly equal in performance, but as always: if it matters, try it and see.Impeditive
In case anyone is wondering like I was you have to cast the image ref to id like so view.layer.contents = (id) myUIImage.CGImage; you should also using bridging cast when using arc view.layer.contents = (__bridge id) myUIImage.CGImage; details were found here https://mcmap.net/q/1067462/-how-do-i-set-the-contents-of-a-calayer-to-a-cgimagerefFabiolafabiolas

© 2022 - 2024 — McMap. All rights reserved.