问题
Those two functions are currently my bottleneck. I am working with very large bitmaps.
How can I improve their performance?
回答1:
You could cache smaller versions of your bitmaps which you create before drawing the first time and then simply draw the downscaled samples instead of the full-blown 15 megapixel stuff.
Then again make sure you are only drawing what is necessary i.e. in 'drawRect: (NSRect) rect' only draw inside the rect (unless absolutely necessary). And try not do perform drawings outside of that method.
回答2:
If you're drawing large background images with content in the foreground that moves, consider using a layer-backed NSView
, adding a layer and setting its background image. You can then draw your content in a other layers (or layer-backed NSViews
) above the background layer, and the view will never need to redraw the background image because it is stored in the GPU's texture memory. Your current image is too large for a single CALayer
(CALayer
s are limited to the maximum OpenGL texture size of 2048 x 2048) so you will probably need to break it up into tiles.
Otherwise, as @iolo mentioned, you should make sure that you only redraw the parts of the view that really need updating.
来源:https://stackoverflow.com/questions/6625654/how-can-i-improve-cgcontextfillrect-and-cgcontextdrawimage-performance