Offscreen rendering

Performance Senior Swift 5.0+ iOS 17+ Reviewed By Alok Singh

Pick the fix

A card with a shadow causes hitches while scrolling. Best fix?

Options
Hint 1 of 1

What does the shadow need to know?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Set layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: 12).cgPath in layoutSubviews

Correct. With an explicit path, the shadow doesn't need an offscreen pass.

Why not the others?
  • B Increase shadowRadius. A larger blur costs more.
  • C Set shouldRasterize on every cell. It re-rasterizes when content changes, which can make things worse.
  • D Turn on Color Blended Layers in release. It's a debug visualisation, not a fix.

30-second answer

Some layer effects can't be drawn in a single pass, so the render server draws them into a separate buffer first, which costs GPU time and can cause hitches. Common causes are shadows without a shadowPath, masks, and corner radius with masksToBounds on layers with content in sublayers. Find them with the Simulator's Debug › Color Off-screen Rendered option, then supply a shadowPath, avoid masks, or pre-render static content.

Practise saying it · 30 s. No recording, just a timer

Explain offscreen rendering.

Say your answer out loud, then check which of these points you covered:

  • Extra render pass
  • Shadows, masks, clipping
  • Color Off-screen Rendered
  • shadowPath

Deep dive

Setting shadowPath lets the shadow be drawn without measuring the layer's alpha.

Color Blended Layers is a different check, for transparency and overdraw.

shouldRasterize caches the result, but helps only if the content doesn't change.

Rounded images can be pre-clipped once instead of masked each frame.

Common mistakes

  • Confusing Color Blended Layers with offscreen rendering.
  • Turning on shouldRasterize for content that animates.

Senior take & follow-ups

Seniors know which effects trigger offscreen passes and use the right debug option.

Follow-up questions

Think of your answer first, then open the hint.

  1. When does shouldRasterize help?

    Show hint

    For complex layers that stay static.

  2. What does Color Blended Layers show?

    Show hint

    Areas where transparent layers are blended.

Suggest a fix if something here is wrong or out of date.