https://github.com/0xwdg/cachedasyncimage
CachedAsyncImage is a Swift Package for asynchronously loading images from the web and caching them.
https://github.com/0xwdg/cachedasyncimage
0xwdg async asyncimage cached cachedasyncimage hacktoberfest images spm swiftui
Last synced: about 1 month ago
JSON representation
CachedAsyncImage is a Swift Package for asynchronously loading images from the web and caching them.
- Host: GitHub
- URL: https://github.com/0xwdg/cachedasyncimage
- Owner: 0xWDG
- License: mit
- Created: 2024-06-25T18:24:47.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-02-20T20:15:49.000Z (4 months ago)
- Last Synced: 2025-05-01T17:04:01.242Z (about 1 month ago)
- Topics: 0xwdg, async, asyncimage, cached, cachedasyncimage, hacktoberfest, images, spm, swiftui
- Language: Swift
- Homepage: https://0xwdg.github.io/CachedAsyncImage/
- Size: 834 KB
- Stars: 11
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# CachedAsyncImage
CachedAsyncImage is a Swift Package for asynchronously loading images from the web and caching them.
[](https://swiftpackageindex.com/0xWDG/CachedAsyncImage)
[](https://swiftpackageindex.com/0xWDG/CachedAsyncImage)
[](https://swift.org/package-manager)
## Requirements
- Swift 5.9+ (Xcode 15+)
- iOS 13+, macOS 10.15+## Installation (Pakage.swift)
```swift
dependencies: [
.package(url: "https://github.com/0xWDG/CachedAsyncImage.git", branch: "main"),
],
targets: [
.target(name: "MyTarget", dependencies: [
.product(name: "CachedAsyncImage", package: "CachedAsyncImage"),
]),
]
```## Installation (Xcode)
1. In Xcode, open your project and navigate to **File** โ **Swift Packages** โ **Add Package Dependency...**
2. Paste the repository URL (`https://github.com/0xWDG/CachedAsyncImage`) and click **Next**.
3. Click **Finish**.## Usage
`CachedAsyncImage` has the exact same API and behavior as `AsyncImage`, so you just have to change this:
```swift
AsyncImage(url: logoURL)
```to this:
```swift
CachedAsyncImage(url: logoURL)
```example:
```swift
import SwiftUI
import CachedAsyncImagestruct ContentView: View {
var body: some View {
VStack {
CachedAsyncImage(
url: URL(
string: "https://wesleydegroot.nl/assets/avatar/avatar.webp"
)
) { image in
image
.resizable()
.frame(width: 250, height: 250)
} placeholder: {
ProgressView()
}
}
.padding()
}
}
```In addition to `AsyncImage` initializers, you have the possibilities to specify the cache you want to use (by default `URLCache.shared` is used), and to use `URLRequest` instead of `URL`:
```swift
CachedAsyncImage(urlRequest: logoURLRequest, urlCache: .imageCache)
``````swift
// URLCache+imageCache.swiftextension URLCache {
static let imageCache = URLCache(memoryCapacity: 512_000_000, diskCapacity: 10_000_000_000)
}
```Remember when setting the cache the response (in this case our image) must be no larger than about 5% of the disk cache (See [this discussion](https://developer.apple.com/documentation/foundation/nsurlsessiondatadelegate/1411612-urlsession#discussion)).
## Contact
๐ฆ [@0xWDG](https://bsky.app/profile/0xWDG.bsky.social)
๐ [mastodon.social/@0xWDG](https://mastodon.social/@0xWDG)
๐ฆ [@0xWDG](https://x.com/0xWDG)
๐งต [@0xWDG](https://www.threads.net/@0xWDG)
๐ [wesleydegroot.nl](https://wesleydegroot.nl)
๐ค [Discord](https://discordapp.com/users/918438083861573692)Interested learning more about Swift? [Check out my blog](https://wesleydegroot.nl/blog/).