swift-cached-remote-image
メモリ & ディスクの二層キャッシュで高速表示する SwiftUI リモート画像
English | 日本語
swift-cached-remote-image
A SwiftUI package for displaying remote images with memory and disk caching.
Overview
swift-cached-remote-image is a package for efficiently loading remote images in SwiftUI with two-layer memory and disk caching. It provides async image loading, retry policies, and fully customizable placeholders.
Features
- ✅ Native SwiftUI API — Seamlessly integrated with SwiftUI
- ✅ Memory & Disk Cache — Automatic two-layer caching for fast display
- ✅ Async Image Loading — Modern concurrency with async/await
- ✅ Flexible ImageSource — Load from URL, URL string, or image ID
- ✅ Image ID Support — Fetch images through
ImageServicefrom a resource endpoint - ✅ Customizable Retry Policy — Fixed count or exponential backoff
- ✅ Customizable Placeholder & Error Views — Fully replaceable UI
- ✅ Cache Management — Separate control of resource and data caches
- ✅ iOS 17.0+ and macOS 14.0+ — Cross-platform support
Requirements
- iOS 17.0+
- macOS 14.0+
- Swift 6.0+
Dependencies
- swift-api-client — HTTP client
Prerequisites
When using .imageId, a REST API that returns responses in the format below is required.
Required API Endpoints
- GET
/images/{imageId}— Fetch image resource - POST
/images— Upload image (Base64-encoded JSON) - DELETE
/images/{imageId}— Delete image
Required Response Format (JSON)
{
"id": "img_123",
"url": "https://example.com/images/photo.jpg"
}
When using .url or .urlString directly, no API server is required.
Installation
Swift Package Manager
Add the following to your Package.swift:
dependencies: [
.package(url: "https://github.com/no-problem-dev/swift-cached-remote-image.git", from: "1.1.5")
]
Or in Xcode:
- File > Add Package Dependencies
- Enter the package URL:
https://github.com/no-problem-dev/swift-cached-remote-image.git - Select version:
1.1.5or later
Quick Start
The simplest usage:
import SwiftUI
import CachedRemoteImage
CachedRemoteImage(
source: .url(URL(string: "https://example.com/image.jpg")!)
)
This handles image loading, caching, and default loading indicator automatically.
Usage
Basic Example
import SwiftUI
import CachedRemoteImage
struct ContentView: View {
var body: some View {
CachedRemoteImage(
source: .url(URL(string: "https://example.com/image.jpg")!)
) { image in
image
.resizable()
.aspectRatio(contentMode: .fit)
} placeholder: {
ProgressView()
}
}
}
Load from URL String
CachedRemoteImage(
source: .urlString("https://example.com/image.jpg")
) { image in
image.resizable()
}
ImageSource Options
// 1. From a URL object
let url = URL(string: "https://example.com/image.jpg")!
CachedRemoteImage(source: .url(url))
// 2. From a URL string (automatically converted)
CachedRemoteImage(source: .urlString("https://example.com/image.jpg"))
// 3. From an image ID (fetches resource via ImageService)
CachedRemoteImage(source: .imageId("img_12345"))
Note:
.imageIdrequiresImageServiceto be injected into the environment.
Using ImageService with Image ID
import APIClient
import CachedRemoteImage
@main
struct MyApp: App {
let imageService: ImageService
init() {
let apiClient = APIClientImpl(baseURL: URL(string: "https://api.example.com")!)
imageService = ImageServiceImpl(
apiClient: apiClient,
imagesPath: "/images",
maxResourceCacheSize: 100
)
}
var body: some Scene {
WindowGroup {
ContentView()
.imageService(imageService)
}
}
}
struct ImageView: View {
let imageId: String
var body: some View {
CachedRemoteImage(
source: .imageId(imageId)
) { image in
image
.resizable()
.aspectRatio(contentMode: .fit)
}
}
}
Cache Configuration
Resource Cache Size
let imageService = ImageServiceImpl(
apiClient: apiClient,
imagesPath: "/images",
maxResourceCacheSize: 200 // up to 200 resource entries
)
Clearing Cache
// Clear resource cache
await imageService.clearResourceCache()
// Clear image data cache
await imageService.clearImageCache()
// Get disk cache size
let cacheSize = await imageService.diskCacheSize()
print("Current cache size: \(cacheSize) bytes")
Per-Image Configuration
CachedRemoteImage(
source: .url(imageURL),
configuration: CachedRemoteImageConfiguration(
cachePolicy: .metadataOnly,
retryPolicy: .exponentialBackoff(maxRetries: 3)
)
) { image in
image.resizable()
}
Available options:
- cachePolicy:
.all(default),.metadataOnly,.imageOnly,.none - retryPolicy:
.none(default),.fixed(count:),.exponentialBackoff(maxRetries:, baseDelay:)
License
This project is released under the MIT License. See the LICENSE file for details.
Support
For bug reports or feature requests, please open an issue on GitHub.
同じカテゴリの OSS — UI / SwiftUI
swift-design-system
Swift PackageSwiftUI 向けの型安全で拡張可能なデザインシステム
swift-ui-routing
Swift PackageSwiftUI 向けの型安全で宣言的なルーティングライブラリ
swift-statable
Swift PackageAsyncValue パターンで Observable な状態管理を実現する Swift マクロ
swift-markdown-view
Swift PackageDesignSystem 統合とシンタックスハイライトを備えた SwiftUI ネイティブな Markdown レンダリング
swift-latex-view
Swift PackageSwiftUI ネイティブな LaTeX 数式レンダリング。LLM 出力にも堅牢
swift-google-slides-view
Swift PackageGoogle Slides API の JSON を SwiftUI で描画。A2A アーティファクトのストリーミングに対応
swift-document-scanner
Swift Package矩形検出・OCR・カメラ撮影を備えた iOS 向けドキュメントスキャン基盤
swift-voice-input
Swift Packageストリーミング認識とフローティングプレビュー UI を備えたプロトコル指向の音声入力