this is my first question here, so hello everyone. I have spent some time now wrapping my mind around the following problem and could not come up with a solution. I'd be happy if someone could point me in the right direction.
I'm using CGContext to draw various layers of graphical representations of live data which should be refreshed at a reasonable framerate of at least 50fps. I want to use a static image as a background. Re-drawing this image using the context for every cycle significantly slows down the frame-rate. I thought about just using UIImageView to display the image once. The problem here is that I want to use the blendmodes provided by CGContext in order to achieve more sophisticated overlays than just setting an alpha-value for each layer. Since the UIImageView displaying the background image is not part of the context, it's not affected by the blendmodes. Does anyone have an idea how to achieve the desired behavior without affecting the frame-rate too much? Thanks for your help.
A call to CGContextDrawImage
with a CGImageRef
with the the same size dimensions as the CGRect is fast. Setup a CGImageRef
with the background image in code that gets run less often than every frame, such as the view's initialization code. If this background image is changing, build the image in the program logic code, not the draw refresh code.
The worst you can do is access the image from a file each time. Some time can be saved by not accessing the CGImageRef
from within a heavier object. A call like [someUIImageObject CGImage]
will add the overhead of a message send, whereas an access to a CGImageRef
type ivar in the same object containing the drawing code will not.
If your source image is not the right size, first draw it into a CGImageRef
at the needed size once, and reuse it in your drawing code.
来源:https://stackoverflow.com/questions/6693977/static-background-for-cgcontext