SwiftUI CachedAsyncImage

repository·main·Indexed 20 days ago

https://github.com/lorenzofiamingo/swiftui-cached-async-image

A drop-in replacement for SwiftUI's native AsyncImage that adds caching capabilities using URLCache. It supports the standard AsyncImage API and allows for custom URLRequest and URLCache configurations.

Tokens
461
Snippets
4
Records
4
Agent score
22%

What's inside swiftui-cached-async-image

  1. Install SwiftUI CachedAsyncImage via Swift Package Manager

    main

    To add this library to your Xcode project:

    1. In Xcode, navigate to FileSwift PackagesAdd Package Dependency...
    2. Paste the repository URL: https://github.com/lorenzofiamingo/swiftui-cached-async-image
    3. Click Next and then Finish.
    https://github.com/lorenzofiamingo/swiftui-cached-async-image
  2. Define a custom image cache using URLCache

    main

    You can extend URLCache to define a dedicated cache for images. When configuring a custom cache, ensure the response (the image) is no larger than approximately 5% of the disk cache capacity to comply with system recommendations.

    Example of defining a large dedicated cache:

    extension URLCache {
        static let imageCache = URLCache(memoryCapacity: 512_000_000, diskCapacity: 10_000_000_000)
    }
    // URLCache+imageCache.swift
    
    extension URLCache {
        
        static let imageCache = URLCache(memoryCapacity: 512_000_000, diskCapacity: 10_000_000_000)
    }