问题描述
在我的iPad应用中,我正在渲染到屏幕外的位图,然后将位图绘制到屏幕上。 (这是因为我想重新使用现有的位图渲染代码。)在iPad 2上,这就像一个魅力,但在带有Retina显示屏的新iPad上,绘制位图真的很慢,即使它的分辨率仍然是使用常规的Quartz 2D函数: CGImageCreate
,并使 CGDataProviderCreateWithData
,32位RGBA格式, kCGImageAlphaNoneSkipLast
。在显示位图的 UIView
中,在 drawRect:
中,我们使用 CGContextDrawImage
将它绘制到 UIGraphicsGetCurrentContext
返回的上下文。
请注意,我甚至没有尝试以双倍分辨率绘制:现在我的分辨率与我在iPad 2上使用的分辨率相同。看起来CoreGraphics内部倍增像素,然后将其发送到GPU,即使<$ c $我正在制作的c> CGImage 可以直接传递给GPU。任何想法?
相当多。更准确(至少在精神上):
- UIKit制作
CGBitmapContext
大小您的视图边界,设备像素数 - 它使该上下文成为当前上下文
- 您绘制
CGImage
进入上下文 - ...所以CG必须重新缩放源图像,并触摸所有目标像素
- 之后'完成绘图,UIKit从位图上下文
- 创建一个
CGImage
,并将其分配给视图的图层内容。
如果你想要这样做,你需要通过删除上面的一些步骤告诉系统这样做。
(UIKit,CoreAnimation和CoreGraphics之间没有提供像您期望的快速路径的链接。)
最简单的方法将会 制作 UIImageView
,并将其图像
设置为 UIImage
包装 CGImageRef
。
或者,设置 view.layer.contents
到您的 CGImageRef
。 (并确保不覆盖 -drawRect:
,不调用 -setNeedsDisplay
,并确保 contentMode
不 UIViewContentModeRedraw
。更容易使用 UIImageView
。)
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?
Pretty much. More accurately (in spirit at least):
- UIKit makes a
CGBitmapContext
the size of your view's bounds, in device pixels - It makes that context the current context
- You draw your
CGImage
into that context - ... so CG has to rescale the source image, and touch all of the destination pixels
- After you're done drawing, UIKit makes a
CGImage
from the bitmap context - and assigns it to the view's layer's contents.
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
.)
这篇关于在Retina iPad上使用CoreGraphics绘制图像很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!