We all faced times when our
UITableView didn’t scroll as we wanted to.
I gathered bellow a simple yet thorough article about what may causes that.
Why is my scroll slow? When you create custom
UITableViewCell and you use a combination of UIViews and UIImages, you may cause UIKit to do
extra Offscreen rendering.
What is Offscreen rendering:
Simply put, is to generate images using Software process neglegting any Hardware acceleration boost.
Types of Offscreen rendering:
- CPU offscreen, using the CPU to generate images:
- Anything inside
- GPU offscreen (Blending), the GPU will have to calculate the final
image, adds shadow, clips, etc…:
- Layers with
- Layers with
Offscreen impact on performance:
Offscreen rendering impacts the performance when its getting done on each frame, in scrolling or animation. but if its done and cached, it may enhance performance.
We still need to create custom
UITableViewCell, fear not the bellow list shows the ways we can achieve that ordered in performance cost.
Customisation methods ordered by performance:
- Use resizable image and normal images
- Use a combination of
- Use Hybrid mode: Use
CoreGraphicsand code to create stretchable images that you will
CALayeronly if you need animation
- Worst! Draw using
Major reasons for slow scroll, And their solutions:
- Slow cell creation.
Offscreencreation of cell takes too long:\ to solve this issue simplify the drawRect.
- Too complex
drawRectfunction:\ Cache any result from
drawRectand reuse it.
- Too complex view hierarchy:\ Try to flatten the view hierarchy.
- Slow cell movements.
- offscreen gets called in each cell:\ Use hybrid mode from above and cache the images.
- too many blending operations:\
Make views opaque and
Tips for fast scrolling:
- Recycle cells.
- Avoid transparent images, views, make them
- Configure the reused cell must be fast.
- Always make most of the views
- Don’t place views on fractions of pixels x = 34.5, always use ceil
or floor on pixels.
- When setting images adjust them (Scale, Clip) after download and
before setting them, don’t let iOS scales them down.
- Scrolling images is faster than views, however the difference is
very small if views are
shouldRasterizeon layer whenever you can since rasters of the
layer will be cached and reused, But remember if you enable
shouldRasterizeon layers that changes their
contentfrequently you will add cause the performance to drop, since iOS will keep rasterising the layer on each change.
- Apple Developer Improving Drawing Performance
- Apple Developer shouldRasterize property