Skip to content

Simple and lightweight library for loading images in a fast way

License

Notifications You must be signed in to change notification settings

NikSativa/SmartImages

Repository files navigation

SmartImages

Simple and lightweight library for loading images in a fast way, because it prioritizes queuing and loading images in the order they are requested and/or if ImageView is waiting for an image to be loaded, it will be prioritized over the others. It uses the native Image object to load images and provides a way to cache them in memory and you can also set a custom cache size.

ImageDownloader

Manager responsible for downloading images from the internet.

ImageCache

Manager responsible for caching images in memory. By default:

  • memory capacity is 40MB
  • disk capacity is 400MB

ImageDownloadQueue

Manager responsible for queuing images to be downloaded and prioritizing the images that are being requested. The priority changes at runtime, so it is calculated before adding to the download queue. The highest priority occurs when the image URL is attached to a UI view (SwiftUI is also supported) and has a queued timestamp that is much closer to the current time.

Example:

  • You have a queue with a limit of 2 tasks at a time.
  • You add 5 tasks with low priority:
for i in 0..<5 {
    imageDownloader.predownload(url: URL(string: "apple.com/image_\(i)")!)
}
  • ImageDownloader is starting download first 2 images immediately.
  • You add 2 tasks which are attached to UI-view
let imageView = UIImageView()
imageView.setImage(withURL: URL(string: "apple.com/image\_\\(99)")!) // new URL
imageView.setImage(withURL: URL(string: "apple.com/image\_\\(3)")!)  // <-- the same URL in queue
  • ImageDownloader did download 1 image and free 1 space in queue.

  • The state is:

    • "image_1 - downloaded
    • "image_2" - in progress
    • "image_3"
    • "image_4"
    • "image_5"
    • "image_3" with View
    • "image_99" with View
  • Prioritization algorithm will do:

    • "image_3" with View and added after "99" - that means timestamp is more close to "now" ("now" - "99".timestamp > "now" - "3".timestamp)
    • "image_99" with View
    • "image_1" - downloaded, no longer needed
    • "image_2" - in progress
    • "image_3" the same URL as already added "with View"
    • "image_4"
    • "image_5"
  • Next task will take "image_3" with View because it is attached to View and that means the User is Waiting this image on his screen.

ImageDownloaderNetwork

Protocol that must be implemented by the app and represents the network layer.

How to use with URLSession

in app:

planeImageView.setImage(withURL: url, placeholder: .image(.planePlaceholder))
planeImageView.setImage(withURL: url, placeholder: .clear)
planeImageView.setImage(withURL: url)

implementation:

import Foundation
import SmartImages
import UIKit

public enum ImageDownloader {
    private static let imageDownloader: ImageDownloading = {
        return SmartImages.ImageDownloader.create(network: ImageDownloaderNetworkAdaptor(),
                                                  cache: .init(folderName: "DownloadedImages"),
                                                  concurrentImagesLimit: 8)
    }()

    public init() {}
}

public extension UIImageView {
    func setImage(withURL url: URL,
                  animated animation: ImageAnimation? = nil,
                  placeholder: ImagePlaceholder = .none,
                  completion: ImageClosure? = nil) {
        let info = ImageInfo(url: url)
        ImageDownloader().download(of: info,
                                   for: self,
                                   animated: animation,
                                   placeholder: placeholder,
                                   completion: completion ?? { _ in })
    }

    func cancelImageRequest() {
        ImageDownloader().cancel(for: self)
    }
}

// MARK: - ImageDownloader + ImageDownloading

extension ImageDownloader: ImageDownloading {
    public var imageCache: ImageCaching? {
        return Self.imageDownloader.imageCache
    }

    public func predownload(of info: ImageInfo,
                            completion: @escaping ImageClosure) {
        Self.imageDownloader.predownload(of: info,
                                         completion: completion)
    }

    public func download(of info: ImageInfo,
                         completion: @escaping ImageClosure) -> AnyCancellable {
        Self.imageDownloader.download(of: info,
                                      completion: completion)
    }

    public func download(of info: ImageInfo,
                         for imageView: ImageView,
                         animated animation: ImageAnimation?,
                         placeholder: ImagePlaceholder = .none,
                         completion: @escaping ImageClosure) {
        Self.imageDownloader.download(of: info,
                                      for: imageView,
                                      animated: animation,
                                      placeholder: placeholder,
                                      completion: completion)
    }

    public func cancel(for imageView: ImageView) {
        Self.imageDownloader.cancel(for: imageView)
    }
}

private struct ImageDownloaderTaskAdaptor: ImageDownloaderTask {
    let task: URLSessionTask

    func start() {
        task.resume()
    }

    func cancel() {
        task.cancel()
    }
}

private struct ImageDownloaderNetworkAdaptor: ImageDownloaderNetwork {
    private let session: URLSession = .shared

    func request(with url: URL,
                 cachePolicy: URLRequest.CachePolicy,
                 timeoutInterval: TimeInterval,
                 completion: @escaping (Result<Data, Error>) -> Void) -> ImageDownloaderTask {
        let dataTask = session.dataTask(with: url) { data, _, error in
            if let error {
                completion(.failure(error))
            } else if let data {
                completion(.success(data))
            } else {
                completion(.failure(NSError(domain: "ImageDownloader", code: 0, userInfo: ["url": url])))
            }
        }

        return ImageDownloaderTaskAdaptor(task: dataTask)
    }
}