Remote downloading and cache of images.
This code is similar to the image cache presented by Apple in Protect mutable state with Swift actors.
- iOS 18
- macOS 15
On macOS, NSImage resizing by drawing into a scaled context decodes the full image first and is slow for large assets. This package uses CGImageSourceCreateThumbnailAtIndex to downscale from the original bytes at decode time (subsampling), then composites according to .contentMode (aspect fit/fill, or fill). iOS continues to use UIImage.preparingThumbnail(of:) when appropriate.
For a concise deep‑dive on why and how we do this, see AGENTS.md.
For SwiftUI clients, this package includes an optional adapter you can use if desired:
ImageLoader(@Observable): loads and exposesstate(idle/loading/success/failure)RemoteImageview: simple view that loads and displays a remote image
Example
import SwiftUI
import ImageCache
struct Avatar: View {
let url = URL(string: "https://example.com/avatar.jpg")!
var body: some View {
RemoteImage(
url: url,
resize: CGSize(width: 120, height: 120),
contentMode: .scaleAspectFill
)
.resizable()
.frame(width: 120, height: 120)
.clipped()
}
}Notes
- Resizing/contentMode are optional; omit
resizeto display the original size. - Internally uses the same downloader + processor and macOS CGImageSource fast path.