Skip to content

janodevorg/ImageCache

Repository files navigation

Swift

Remote downloading and cache of images.

This code is similar to the image cache presented by Apple in Protect mutable state with Swift actors.

Platforms

  • iOS 18
  • macOS 15

macOS Thumbnail Performance

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.

SwiftUI Adapter (Optional)

For SwiftUI clients, this package includes an optional adapter you can use if desired:

  • ImageLoader (@Observable): loads and exposes state (idle/loading/success/failure)
  • RemoteImage view: 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 resize to display the original size.
  • Internally uses the same downloader + processor and macOS CGImageSource fast path.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •