Contents

prepareThumbnail(of:completionHandler:)

Creates a thumbnail image at the specified size asynchronously on a background thread.

Declaration

func prepareThumbnail(of size: CGSize, completionHandler: @escaping  @Sendable (UIImage?) -> Void)
func byPreparingThumbnail(ofSize size: CGSize) async -> UIImage?

Parameters

  • size:

    The desired size of the thumbnail.

  • completionHandler:

    The completion handler to call when the thumbnail is ready. The handler executes on a background thread.

    The completion handler takes the following parameters:

    thumbnail

    A new thumbnail image. This parameter is nil if the original image isn’t backed by a Cgimage or if the image data is corrupt or malformed.

Discussion

When displaying an image in a UIImageView, you can use the view’s contentMode property to clip or scale the image automatically. But when the native image size is much larger than the bounds of the view, decoding the full size image creates unnecessary memory overhead. By creating a thumbnail image at a specified size with this method, you avoid the overhead of decoding the image at its full size.

This method asynchronously creates the thumbnail image on a background thread and calls the completion handler on that thread. If your app updates the UI in the completion handler, schedule the UI update on the main thread.

See Also

Loading images for display