62328

Is it possible to persist the contents of a CALayer between drawInContext calls?

Question:

Are there any built in abilities to maintain the contents of a CALayer between drawLayer:inContext: calls? Right now I am copying the layer to a buffer and redrawing an image from the buffer every time I called back in drawLayer:inContext: but I'm wondering if CALayer has a way to do this automatically...

Answer1:

I don't believe so. The 'drawInContext' will clear the underlying buffer so that you can draw to it. However, if you forego the drawInContext or drawRect methods, you can set your layer.contents to a CGImage and that will be retained.

I personally do this for almost all of my routines. I overwrite - (void) setFrame:(CGRect)frame to check to see if the frame size has changed. If it has changed I redraw the image using my normal drawing routines but into the context: UIGraphicsBeginImageContextWithOptions(size, _opaque, 0);. I can then grab that image and set it to the imageCache: cachedImage = UIGraphicsGetImageFromCurrentImageContext();. Then I set the layer.Contents to the CGImage. I use this to help cache my drawings, especially on the new iPad which is slow on many drawing routines the iPad 2 doesn't even blink on.

Other advantages to this method: You can share cached images between views if you set up a separate, shared cache. This can really help your memory footprint if you manage your cache well. (Tip: I use NSStringFromCGSize as a dictionary key for shared images). Also, you can actually spin off your drawing routines on a different thread, and then set your layer contents when it's done. This prevents your drawing routines from blocking the main thread (the current image may be stretched in this case though until the new image is set).

Recommend

  • how to change background color for nimbus look and feel using java
  • Check if an image is already in gallery and retrieve it
  • What is an instance of an NSObject
  • file_get_contents() and Curl
  • Custom Shaped progress View
  • multi threaded program is blocking
  • QBrush - scale a standard fill stipple pattern?
  • PayPal IPN OPENSSL error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure
  • Modify application instruction in PIN
  • mmap big endian vs. little endian
  • Stream 2 videos simultaneously Swift
  • Dynamic Programming - top-down vs bottom-up
  • Regex: match only outside parenthesis (so that the text isn't split within parenthesis)?
  • Internet explorer and removeChild()
  • Chinese and Japanese character encoding issues when exporting HTML to PDF
  • How to make NSView's background image not repeat?
  • Does using global variables increase or decrease performance, in C code compiled for ARM7?
  • how to release bitmap memory in android
  • OpenGL ES 2.0 / MonoTouch: Texture is colorized red
  • Publishing commands to device in IBM IoT using MQTT in Java
  • Memory leak when filtering with Core image
  • Reduce Mono runtime size
  • boost::format vs sprintf
  • WPF Image control clipping
  • How to create a Unix-domain socket with specific access permissions
  • GAE: Way to get reference to an HttpSession from its ID?
  • Moving Android View and preventing onDraw to be called over and over again
  • Blackberry - Custom EditField Cursor
  • Moving mysql files across servers
  • Update CALayer sublayers immediately
  • Spray.io: When (not) to use non-blocking route handling?
  • swift auto completion not working in Xcode6-Beta
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • Eraser for UIBezierPath
  • Cannot Parse HTML Data Using Android / JSOUP
  • C# - Getting references of reference
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • reshape alternating columns in less time and using less memory