c2p-cmd/CachedNetworkImage
[](https://swiftpackageindex.com/c2p-cmd/CachedNetworkImage) [](https://swiftpackageindex.com/c2p-cmd/CachedNetworkImage)
Features
- Image Caching: Avoids unnecessary network requests by caching downloaded images.
- Customizable Placeholders: Display placeholders while the image is loading.
- Error Handling: Define custom views for handling errors.
- Resizing Options: Optionally download high-resolution images or resize for performance optimization.
- Cache Expiration: Configurable time-to-live (TTL) for cached images.
Installation
Swift Package Manager
- In Xcode, go to File > Add Packages.
- Enter the URL of this repository.
- Select the library and integrate it into your project.
Usage
Basic Example
import SwiftUI
import CachedNetworkImage
struct ContentView: View {
var body: some View {
CachedNetworkImage(
url: URL(string: "https://example.com/image.jpg"),
imageView: { $0.resizable() },
placeholder: { ProgressView() },
errorView: { error in Text("Error: \(error.localizedDescription)") }
)
.scaledToFit()
.frame(width: 200, height: 200)
}
}Parameters
|Parameter | Type | Description | |----------|------|-------------| |url | URL? | URL of image to load | |highRes | Bool | Whether to down size image after fetch | |cacheTime | TimeInterval | The time to live for the cached image. Default is 1 hour| |imageView | (Image) -> some View | ViewBuilder when image is passed | |placeholder | () -> some View | ViewBuilder when image is being loaded. Defaults to ProgressView | |errorView | (any Error) -> some View | ViewBuilder when error occurs |
Advanced Usage
Custom Placeholder and Error Views
CachedNetworkImage(
url: URL(string: "https://example.com/image.jpg"),
keepFullRes: false,
cacheTime: 600, // 10 minutes
imageView: { $0.resizable().aspectRatio(contentMode: .fit) },
placeholder: {
VStack {
ProgressView()
Text("Loading image...")
}
},
errorView: { error in
VStack {
Image(systemName: "exclamationmark.triangle.fill")
.foregroundColor(.red)
Text("Failed to load image")
}
}
)License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a new branch with your feature or bug fix.
- Submit a pull request with detailed information about your changes
Package Metadata
Repository: c2p-cmd/CachedNetworkImage
Stars: 1
Forks: 0
Open issues: 0
Default branch: master
Primary language: swift
License: Apache-2.0
README: README.md