Get Who Cares

Where Home Entertainment Meets the Next-Gen tech

Lightweight and customisable async image loading in SwiftUI

If you have ever tried to make a new image on a mobile device, you have probably been annoyed by the app taking a long time to load, or by the image not appearing at all. In this article, I will be showcasing a tool that I have created to make loading images in iOS significantly faster.

SwiftUI is a framework for building user interfaces in Swift. It supports the basic functionality of the UIKit framework, such as presenting views for input and output, and handling events such as touch and swipe. The framework also provides additional functionality such as adding dynamic content to views and managing and manipulating data.

Asynchronous programming is a great tool to write async apps, but the SwiftUI library that ships with Xcode does not support using an Image with an AsyncImage. This post looks at how to get around that limitation and create a custom importer for SwiftUI that will support asynchronous loading of images.

Lightweight-and-customisable-async-image-loading-in-SwiftUI

1630509878_997_Lightweight-and-customisable-async-image-loading-in-SwiftUI

In Swift UI, you may download and show pictures asynchronously. Progress indicators, placeholders, and image transitions are all supported.

Declaratively download and show pictures asynchronously. Progress indicators, placeholders, and image transitions are all supported. Options for caching that are flexible.

Flexible caching and picture retrieval are carried out in the background. As shown in Example, this is currently being tested using a simple List.

This is a work in progress; if you have any suggestions or comments, please open an issue or contact us.

Lightweight-and-customisable-async-image-loading-in-SwiftUI

“However, AsyncImage”

Although it’s wonderful that Apple has now officially supported async pictures, there are a few caveats:

RemoteImageView, unlike AsyncImage, is asynchronous.

  • From iOS 13 forward, it is supported.
  • Caching is supported (in memory, on disk, and custom)
  • Supports configurable transitions and progress indicators (including download fraction).
  • Logging is extensive and customizable.

Configuration

Debug logging may be enabled or disabled.

SwURLDebug.loggingEnabled = true SwURLDebug.loggingEnabled = true SwURLDebug.loggingEn

Choose between global permanent caching and in-memory caching (the default).

setImageCache(); SwURL.setImageCache(); SwURL.setImageC (type: .inMemory)

setImageCache(); SwURL.setImageCache(); SwURL.setImageC (type: .persistent)

… or use ImageCacheType to create your own caching solution.

setImageCache(type:.custom(ImageCacheType)) SwURL.setImageCache(type:.custom(ImageCacheType)) SwURL.setImageCache(type:.custom

Usage

A URL, placeholder Image (default nil), and a.custom ImageTransitionType are used to initialize the RemoteImageView (default .none).

A resized picture will be downloaded in the background and a placeholder will be shown while the image is loading, transitioning to the downloaded image once it is complete.

In a List, LandmarkRow is utilized.

Example

var landmark: struct LandmarkRow: View var landmark: struct LandmarkRow: View struct LandmarkRow: View var landmark Landmark body variable: some HStack may be seen here. ( url: landmark.image ) RemoteImageView( url: landmark.image ) placeholder, URL Image: Image.init(“placeholder avatar”), transition:.custom(transition:.opacity, animation:.easeOut(duration: 0.5)) ).imageProcessing(image in return image.resizable().renderingMode(.template).aspectRatio(contentMode:.fit) ).progress(progress in return MyProgressBarView(progress: progress) ).progress( Spacer() Text(verbatim: landmark.name)

Parameters that are available

Name Description Default
url The image’s URL from a distant source. none
placeholderImage (optional) Image to show when fetching and decoding remote image data. nil
transition (optional) transition to occur when the loaded picture is shown. nil

Indicators of Progress / Loading

Using the progress function. As the picture loads, put a loading indication on top of the placeholder. The provided loading indication will disappear after the picture has completed downloading. Return some Views by calling progress on your RemoteImageView.

Example

).progress(progress in return MyProgressBarView(progress: progress) ).progress(progress in return MyProgressBarView(progress: progress) ).progress(progress in return MyProgressBar

Processing of Photographs

Using the function imageProcessing is a term that refers to the process of converting an image into Once your placeholder and loaded pictures have been loaded, process them. Resize, crop, resize, resize, resize, resize, resize, resize, resize, re Return some View by using imageProcessing on your RemoteImageView.

Example

).imageProcessing(image in return image.resizable().renderingMode(.template).aspectRatio(contentMode:.fit)

You now have the ability to return any View you desire. By default, resizable() is applied to all pictures in RemoteImageView.

Swift Package Manager is the only way to get SwURL.

  • Launch Xcode.
  • Go to File > Swift Packages > Add Package Dependency…
  • In the search box, paste this Github Repo URL ( https://github.com/cmtrounce/SwURL ).
  • From the search results, choose the SwURL repo.
  • To clone a branch or version, choose it. The most current version is the most stable, however for the most up to date modifications, you may select branches master and develop.
  • Confirm and have fun!

Join the SwURL Gitter community and contact me directly at https://gitter.im/SwURL-package/community. For a faster response time, this is a good option.

You may also find me on Twitter at https://twitter.com/ctrounce94 and contact me there.

GitHub

https://github.com/cmtrounce/SwURL

In this article, we’re going to learn how to use the UIImage loading library to load images in a SwiftUI project without a DOM.. Read more about swiftui image url string and let us know what you think.

Related Tags

  • swiftui async load
  • swiftui async load image
  • cannot find ‘asyncimage’ in scope swiftui
  • kingfisher swiftui
  • swiftui image from url