Skip to content

avito-tech/ImageSource

Repository files navigation

ImageSource is an image abstraction. It is intended to represent an image that can come from many different sources.

Some examples of the sources are:

  • file system (LocalImageSource)
  • network (RemoteImageSource)
  • user's photo library (PHAssetImageSource)

It allows you to retrieve the actual bitmap in a simple and efficient way using the unified API and supports retrieval cancellation. It is also designed to be platform-independent, so you can use it both on iOS and macOS.

To integrate ImageSource into your Xcode project using CocoaPods, specify it in your Podfile:

use_frameworks!

target '<Your Target Name>' do
    pod 'ImageSource'
end

Then, run the following command:

$ pod install
let package = Package(
    // 4.0.0 
    dependencies: [
        .package(url: "https://github.com/avito-tech/ImageSource.git", from: "4.0.0")
    ],
    // ...
)

To present ImageSource in UIImageView, you should use extension method that comes with ImageSource/UIKit pod:

func setImage(
    fromSource: ImageSource?,
    size: CGSize? = nil,
    placeholder: UIImage? = nil,
    placeholderDeferred: Bool = false,
    adjustOptions: ((_ options: inout ImageRequestOptions) -> ())? = nil,
    resultHandler: ((ImageRequestResult<UIImage>) -> ())? = nil)
    -> ImageRequestId?

In most cases, however, you would want to just use its simplest version, passing only the first parameter:

imageView.setImage(fromSource: imageSource)

To get image data use ImageSource.fullResolutionImageData(completion:):

imageSource.fullResolutionImageData { data in
    try? data?.write(to: fileUrl)
}

To get image size use ImageSource.imageSize(completion:):

imageSource.imageSize { size in
    // do something with size
}