Fix: Blurry UIView to UIImage Capture on Retina Display

Fix: Blurry UIView to UIImage Capture on Retina Display

Written by

in

Quick answer: UIGraphicsBeginImageContext always creates a 1x image, so it looks blurry on Retina (2x/3x) screens. Use UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, 0.0) — scale 0.0 automatically uses the screen’s scale — or use UIGraphicsImageRenderer on iOS 10+.

Capturing a UIView to a UIImage with UIGraphicsBeginImageContext looks fine on non-Retina simulators but produces a noticeably blurry, low-resolution image on any Retina iPhone or iPad. The image is half or one-third the expected pixel size and gets stretched when displayed or saved.

Why this happens

UIGraphicsBeginImageContext(_:) is a legacy convenience function that always creates a bitmap context with a scale factor of 1.0. One point equals one pixel.

Retina displays use a scale of 2.0 or 3.0 — one point equals 2×2 or 3×3 pixels. When you create a 1x context from a Retina view, you are only capturing a quarter (on 2x) or a ninth (on 3x) of the pixels the screen actually renders. UIKit then stretches that small image back to the view’s point size, which looks soft and blurry compared to the sharp content around it.

The fix is to create the context at the screen’s native scale. UIGraphicsBeginImageContextWithOptions(_:opaque:scale:) and the modern UIGraphicsImageRenderer both let you do this. Passing 0.0 for scale tells UIKit to use UIScreen.main.scale automatically, so the same code works correctly on 1x, 2x, and 3x devices.

How to fix it

1. Use UIGraphicsBeginImageContextWithOptions with scale 0.0

This is the direct replacement for the blurry code and works on all iOS versions. It keeps render(in:) which captures the layer tree accurately.

Replace this:

// Blurry on Retina - fixed scale of 1.0
UIGraphicsBeginImageContext(view.bounds.size)
view.layer.render(in: UIGraphicsGetCurrentContext()!)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

With this:

Swift:

import UIKit

func image(with view: UIView) -> UIImage? {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, 0.0)
    defer { UIGraphicsEndImageContext() }
    guard let context = UIGraphicsGetCurrentContext() else { return nil }
    view.layer.render(in: context)
    return UIGraphicsGetImageFromCurrentImageContext()
}

Objective-C:

#import <QuartzCore/QuartzCore.h>

+ (UIImage *)imageWithView:(UIView *)view {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

Key parameters:

  • size: Use view.bounds.size, not view.frame.size. bounds is in the view’s own coordinate space and avoids offset errors if the view is transformed.
  • opaque: Pass view.isOpaque (or view.opaque in Objective-C). Use false if the view has transparency or rounded corners you want to preserve. Use true only for fully opaque views — it is slightly faster.
  • scale: Pass 0.0. This means “use the main screen’s scale”. Do not hardcode 2.0 or 3.0 — 0.0 adapts to the current device and to future scales.

This alone fixes the blur. The returned UIImage will have scale set to 2.0 or 3.0 and size still in points, so a 100x100 point view produces a 200x200 or 300x300 pixel image.

2. Use drawViewHierarchyInRect:afterScreenUpdates: for a faster, more complete snapshot (iOS 7+)

view.layer.render(in:) does not capture everything. It can miss visual effects, UIVisualEffectView, MKMapView, SKView, and content that is rendered on the GPU. It also does not respect afterScreenUpdates.

Since iOS 7, drawViewHierarchyInRect(_:afterScreenUpdates:) is the recommended way to snapshot a view hierarchy. It is also significantly faster because it can use the existing rendered hierarchy.

Swift:

func snapshot(of view: UIView) -> UIImage? {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, 0.0)
    defer { UIGraphicsEndImageContext() }
    // Use false if you are calling from viewDidLoad or during an animation
    // Use true if you just updated the view and need the latest layout
    view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
    return UIGraphicsGetImageFromCurrentImageContext()
}

Objective-C:

+ (UIImage *)snapshotOfView:(UIView *)view {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
    UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return snapshot;
}

When to use which:

  • Use drawHierarchy(in:afterScreenUpdates:) if you need blur, vibrancy, maps, or you want the fastest snapshot and you are on the main thread.
  • Keep layer.render(in:) if you need to snapshot off the main thread or from a background context, or if drawHierarchy returns false (it can fail if called offscreen). render(in:) is more reliable off the main thread.

Important: drawHierarchy(in:afterScreenUpdates:) must be called on the main thread. If you pass afterScreenUpdates: true while the view has pending layout or animations, it will wait for the next run loop. If you are snapshotting immediately after adding a view, pass true; otherwise false is safer to avoid a blank image.

3. Use UIGraphicsImageRenderer on iOS 10+ (modern, recommended)

If your deployment target is iOS 10 or later, UIGraphicsImageRenderer is cleaner and handles scale, color space, and memory automatically. You do not need to call UIGraphicsBeginImageContext / UIGraphicsEndImageContext at all.

import UIKit

func rendererImage(of view: UIView) -> UIImage {
    let format = UIGraphicsImageRendererFormat.default()
    format.opaque = view.isOpaque
    format.scale = UIScreen.main.scale // or 0.0 is not needed; default is screen scale

    let renderer = UIGraphicsImageRenderer(bounds: view.bounds, format: format)
    return renderer.image { context in
        // Choose one:
        view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
        // or: view.layer.render(in: context.cgContext)
    }
}

Even simpler, the default format already uses the screen scale:

func simpleRendererImage(of view: UIView) -> UIImage {
    let renderer = UIGraphicsImageRenderer(bounds: view.bounds)
    return renderer.image { _ in
        view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
    }
}

UIGraphicsImageRenderer also avoids common mistakes like forgetting UIGraphicsEndImageContext() and automatically supports wide color when needed.

If your image is still blurry after these changes, check two things: first, make sure you are not displaying the image in a UIImageView with a different contentMode that scales it, and second, make sure you are not saving it as JPEG with high compression or resizing it with a 1x graphics context elsewhere in your pipeline.

How to prevent it

  • Never use UIGraphicsBeginImageContext(_:) for view snapshots. Always use UIGraphicsBeginImageContextWithOptions with scale: 0.0 or UIGraphicsImageRenderer.
  • Use view.bounds for the size, not view.frame, and snapshot after layout (view.layoutIfNeeded() if needed).
  • Prefer UIGraphicsImageRenderer for all new code — it defaults to the correct scale and is less error-prone.
  • Test snapshots on a 3x device or simulator (e.g., iPhone 15 Pro) and log image.scale and image.size vs image.cgImage?.width to verify pixel dimensions.
  • Keep snapshot code on the main thread when using drawHierarchy(in:afterScreenUpdates:).

FAQ

Why should I pass 0.0 for scale instead of 2.0 or 3.0?

0.0 is a special value that means “use the scale of the main screen”. Hardcoding 2.0 will still be blurry on 3x devices (iPhone Plus/Pro models) and will waste memory on 2x devices if you hardcode 3.0. 0.0 adapts automatically and also works correctly with external displays or when the trait changes.

Should I use render(in:) or drawHierarchy(in:afterScreenUpdates:)?

Use drawHierarchy(in:afterScreenUpdates:) for most UI snapshots — it captures UIVisualEffectView, AVPlayerLayer, and other GPU-rendered content and is faster. Use layer.render(in:) only if you must render off the main thread or if drawHierarchy fails to produce an image (it returns a Bool you can check).

I fixed the scale but the saved PNG/JPEG is still blurry — why?

Check how you save and display it. Saving with jpegData(compressionQuality: 0.5) will introduce compression artifacts that look like blur. Also, if you create a thumbnail with another UIGraphicsBeginImageContext call without the correct scale, you will reintroduce the problem. Verify the final file’s pixel size: a 100pt view on a 3x screen should be 300×300 pixels.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.