Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Apple's documentation is unfortunately conflicting as the docs on -setNeedsDisplayInRect do not indicate whether the method works in practice. Based on my own experience, <a href="http://developer.apple.com/library/ios/#qa/qa1708/_index.html" rel="noreferrer">this technote</a> sets it straight:</p> <blockquote> <p>Note that, because of the way that iPhone/iPod touch/iPad updates its screen, the entire view will be redrawn if you call -setNeedsDisplayInRect: or -setNeedsDisplay:.</p> </blockquote> <p>That being said, there are a number of things you can look into if you think that you are hitting a wall due to redundant drawing.</p> <ul> <li>If drawing images, the biggest performance improvement you can make is to use images of the same dimensions at which you draw. If they're not, try to cache your image by rendering it to some offscreen bitmap context and bring it back later on.</li> <li>Check out the <code>shouldRasterize</code> property on CALayer. This can be a godsend if you are trying to manipulate a layer whose sublayers potentially constitute a complex layer hierarchy. Be sure to check out how you're doing in Instruments by ticking the <code>Color Hits Green and Misses Red</code> box in the Core Animation instrument. If you see a lot of red, chances are using <code>shouldRasterize</code> is hurting more than it's helping.</li> <li>Even better than <code>shouldRasterize</code> is to flatten your layer hierarchy, as then you can avoid the extra overhead that <code>shouldRasterize</code> incurs when flattening your layer hierarchy real time. Of course this is not always possible, but don't be afraid to try :)</li> <li>If you're drawing images, try experimenting with your <a href="https://developer.apple.com/library/mac/documentation/graphicsimaging/reference/CGContext/Reference/reference.html#//apple_ref/doc/c_ref/CGBlendMode" rel="noreferrer">blending mode</a>. If you happen to be drawing opaque images, there's no need to be using normal source over methods (which use both read/write bandwidth). Try kCGBlendModeCopy, which allows you to eliminate read bandwidth overhead.</li> <li>Check out CGLayerRef, which allows you to cache Core Graphics output across various calls to your drawing methods. My experience is that, unless you're doing some hardcore pixel pushing, that this ends up being more costly than just redrawing. See <a href="http://iosptl.com/posts/cglayer-no-longer-recommended/" rel="noreferrer">this</a> for an interesting read.</li> <li>Above all, Instruments is your friend. Check out a couple videos from past WWDCs (<a href="https://www.google.com/webhp?hl=en&amp;tab=ww#hl=en&amp;sugexp=les;&amp;gs_nf=3&amp;tok=CcQE2wSaeHi7mmYfl3mWoA&amp;cp=6&amp;gs_id=m&amp;xhr=t&amp;q=wwdc%20session%20videos&amp;pf=p&amp;output=search&amp;sclient=psy-ab&amp;oq=wwdc%20s&amp;gs_l=&amp;pbx=1&amp;bav=on.2,or.r_gc.r_pw.r_cp.r_qf.&amp;fp=e7dd2dbb28ac9867&amp;bpcl=35466521&amp;biw=1207&amp;bih=681" rel="noreferrer">2012, 2011, and 2010</a>); they all have great info about how to fine-tune performance.</li> </ul> <p>Please feel free to ask any further questions if something I've said makes little sense.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload