Skip to content

Commit

Permalink
Update README for New Version (#22)
Browse files Browse the repository at this point in the history
* Add convenience initializer

* Update README for new version

* Use consistent style

* Add documentation catalog

* Additional documentation
  • Loading branch information
mgacy authored Jul 10, 2023
1 parent 7352691 commit f926a9e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 24 deletions.
44 changes: 20 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# ImageFetcher

ImageFetcher is lightweight image loading library build on top of `OperationQueue`. It is optimizes for scroll view performance by decompressing images in the background and exposing a configuration option for rounding corners of the image.
ImageFetcher is lightweight image loading library. It is optimizes for scroll view performance by decompressing images in the background and exposing a configuration option for rounding corners of the image.

## 📱 Requirements

Swift 5.1x toolchain with Swift Package Manager, iOS 12
Swift 5.5 toolchain with Swift Package Manager, iOS 13

## 🖥 Installation

Expand All @@ -13,34 +13,32 @@ Swift 5.1x toolchain with Swift Package Manager, iOS 12
Add `ImageFetcher` to your `Packages.swift` file:

```swift
.package(url: "https://github.com/Mobelux/ImageFetcher.git", from: "1.0.0"),
.package(url: "https://github.com/Mobelux/ImageFetcher.git", from: "2.0.0"),
```

## ⚙️ Usage

Intialize `ImageFetcher` with a `Cache`:

```swift
let fetcher = try ImageFetcher(DiskCache(storageType: .temporary(nil)))
let fetcher = ImageFetcher(try DiskCache(storageType: .temporary(nil)))
```

Optionally initialize with a queue and maximum concurrent download count:
Optionally initialize with a session configuration and maximum concurrent image processing operation count:

```swift
let fetcher = try ImageFetcher(DiskCache(storageType: .temporary(nil)), queue: OperationQueue(), maxConcurrent: 5)
let sessionConfiguration = URLSessionConfiguration.default
sessionConfiguration.timeoutIntervalForResource = 20
let fetcher = ImageFetcher(try DiskCache(storageType: .temporary(nil)),
sessionConfiguration: sessionConfiguration,
maxConcurrent: 5)
```

To fetch an image from the web:

```swift
let config = ImageConfiguration(url: URL(string: "https://via.placeholder.com/150")!)
fetcher.load(config) { result in
switch result {
case .success(let imageResult):
let image = imageResult.value
default: break
}
}
let image = try await fetcher.load(config).value
```

Fetch an image with configuration options and robust handling:
Expand All @@ -51,18 +49,16 @@ let config = ImageConfiguration(url: URL(string: "https://via.placeholder.com/15
constrain: true,
cornerRadius: 10.0,
scale: 1)
fetcher.load(config) { result in
switch result {
case .success(let imageResult):
switch imageResult {
case .cached(let image):
/// handle image coming from cache
case .downloaded(let image):
/// handle newly downloaded image
}
case .failure(let error):
/// handle error
do {
let imageSource = try await fetcher.load(config)
switch imageSource {
case .cached(let image):
/// handle image coming from cache
case .downloaded(let image):
/// handle newly downloaded image
}
} catch {
/// handle error
}
```

Expand Down
11 changes: 11 additions & 0 deletions Sources/ImageFetcher/Documentation.docc/ImageFetcher.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# ``ImageFetcher``

Fetch images from the Web.

## Overview

ImageFetcher is lightweight image loading library. It is optimizes for scroll view performance by decompressing images in the background and exposing a configuration option for rounding corners of the image.

## Topics

### Essentials
15 changes: 15 additions & 0 deletions Sources/ImageFetcher/Extensions/Image+Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ import UIKit
#endif

public extension Image {
/// Returns the optimal size for the image fitting the given sizes.
/// - Parameters:
/// - maxSize: The maximum image size.
/// - size: The target image size.
/// - Returns: The optimal size for the image based on the provided size constraints.
private func sizeFittingSize(_ maxSize: CGSize, size: CGSize) -> CGSize {
let originalAspectRatio = size.width / size.height
if size.width > size.height {
Expand All @@ -57,6 +62,13 @@ public extension Image {
}
}

/// Decompresses the image using the given settings.
/// - Parameters:
/// - newSize: The new size for the image.
/// - constrain: A Boolean indicating whether the image should be constrained by its aspect ratio.
/// - cornerRadius: The image corner radius.
/// - scale: The factor by which the image should be scaled.
/// - Returns: The decompressed image.
func decompressed(
_ newSize: CGSize? = nil,
constrain: Bool = false,
Expand Down Expand Up @@ -110,6 +122,9 @@ public extension Image {
}
}

/// Edits the image using the given configuration.
/// - Parameter configuration: The configuration to use.
/// - Returns: The edited image, if successful.
func edit(configuration: ImageConfiguration) -> Image? {
guard configuration.size != nil || configuration.cornerRadius > 0 else {
return self
Expand Down
2 changes: 2 additions & 0 deletions Sources/ImageFetcher/Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
#if os(macOS)
import AppKit

/// A platform-independent representation of an image.
public typealias Image = NSImage
#else
import UIKit

/// A platform-independent representation of an image.
public typealias Image = UIImage
#endif
18 changes: 18 additions & 0 deletions Sources/ImageFetcher/ImageFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ public final class ImageFetcher: ImageFetching {
self.networking = networking
self.imageProcessor = imageProcessor
}

/// Creates an image fetcher.
/// - Parameters:
/// - cache: A type that caches data.
/// - sessionConfiguration: A configuration object that specifies behaviors for the `URLSession`
/// instance used to fetch images.
/// - maxConcurrent: The maximum number of image processing operations that can run at the same
/// time.
public convenience init(
_ cache: Cache,
sessionConfiguration: URLSessionConfiguration = .default,
maxConcurrent: Int = 2
) {
self.init(
cache,
networking: Networking(sessionConfiguration),
imageProcessor: ImageProcessor(maxConcurrent: maxConcurrent))
}
}

// MARK: - Public API Methods
Expand Down

0 comments on commit f926a9e

Please sign in to comment.