All Projects → hyperoslo → Imaginary

hyperoslo / Imaginary

Licence: other
🦄 Remote images, as easy as one, two, three.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Imaginary

Material Ui Image
Material style image with loading animation
Stars: ✭ 178 (-68.04%)
Mutual labels:  image, loader
React Image
React.js <img> tag rendering with multiple fallback & loader support
Stars: ✭ 917 (+64.63%)
Mutual labels:  image, loader
Inject Loader
💉📦 A Webpack loader for injecting code into modules via their dependencies.
Stars: ✭ 474 (-14.9%)
Mutual labels:  loader
React Native Image Crop Picker
iOS/Android image picker with support for camera, video, configurable compression, multiple images and cropping
Stars: ✭ 5,261 (+844.52%)
Mutual labels:  image
Image2ascii
🌁 Convert image to ASCII
Stars: ✭ 504 (-9.52%)
Mutual labels:  image
Babel Loader
📦 Babel loader for webpack
Stars: ✭ 4,570 (+720.47%)
Mutual labels:  loader
Easywatermark
🔒 🖼 Securely, easily add a watermark to your sensitive photos. 安全、简单地为你的敏感照片添加水印,防止被小人泄露、利用
Stars: ✭ 519 (-6.82%)
Mutual labels:  image
Gaussianblur
An easy and fast library to apply gaussian blur filter on any images. 🎩
Stars: ✭ 473 (-15.08%)
Mutual labels:  image
Cameraengine
🐒📷 Camera engine for iOS, written in Swift, above AVFoundation. 🐒
Stars: ✭ 554 (-0.54%)
Mutual labels:  image
Search By Image
Browser extension for reverse image search, available for Edge, Chrome and Firefox
Stars: ✭ 500 (-10.23%)
Mutual labels:  image
React Native Fit Image
Responsive image component to fit perfectly itself.
Stars: ✭ 539 (-3.23%)
Mutual labels:  image
React Viewer
react image viewer, supports rotation, scale, zoom and so on
Stars: ✭ 502 (-9.87%)
Mutual labels:  image
Retinajs
JavaScript, SCSS, Sass, Less, and Stylus helpers for rendering high-resolution image variants
Stars: ✭ 4,470 (+702.51%)
Mutual labels:  image
Lgphotobrowser
照片浏览器,相册选择器,自定义照相机(支持单拍、连拍)
Stars: ✭ 527 (-5.39%)
Mutual labels:  image
Compressor
An easy to use and well designed image compress library for Android, based on Android native image library. Put forward a framework for quick switch from different compress algorithm.
Stars: ✭ 476 (-14.54%)
Mutual labels:  image
Sdwebimage
Asynchronous image downloader with cache support as a UIImageView category
Stars: ✭ 23,928 (+4195.87%)
Mutual labels:  image
Synthesizing
Code for paper "Synthesizing the preferred inputs for neurons in neural networks via deep generator networks"
Stars: ✭ 474 (-14.9%)
Mutual labels:  image
Renderhelp
⚡️ 可编程渲染管线实现,帮助初学者学习渲染
Stars: ✭ 494 (-11.31%)
Mutual labels:  image
Swift Video Generator
Stars: ✭ 517 (-7.18%)
Mutual labels:  image
React Native Syan Image Picker
React-Native 多图片选择 支持裁剪 压缩
Stars: ✭ 556 (-0.18%)
Mutual labels:  image

Imaginary

CI Status Version License Platform Swift

Brick Icon

Table of Contents

Description

Using remote images in an application is more or less a requirement these days. This process should be easy, straight-forward and hassle free, and with Imaginary, it is. The library comes with a narrow yet flexible public API and a bunch of built-in unicorny features:

  • [x] Asynchronous image downloading
  • [x] Memory and disk cache based on Cache
  • [x] Image decompression
  • [x] Default transition animations
  • [x] Possibility to pre-process and modify the original image
  • [x] Works on any view, including ImageView, Button, ...
  • [x] Supports iOS, tvOS, macOS

Usage

In the most common case, you want to set remote image from url onto ImageView. Imaginary does the heavy job of downloading and caching images. The caching is done via 2 cache layers (memory and disk) to allow fast retrieval. It also manages expiry for you. And the good news is that you can customise most of these features.

Basic

Set image with URL

Simply pass URL to fetch.

let imageUrl = URL(string: "https://avatars2.githubusercontent.com/u/1340892?v=3&s=200")
imageView.setImage(url: imageUrl)

Use placeholder

Placeholder is optional. But the users would be much pleased if they see something while images are being fetched.

let placeholder = UIImage(named: "PlaceholderImage")
let imageUrl = URL(string: "https://avatars2.githubusercontent.com/u/1340892?v=3&s=200")

imageView.setImage(url: imageUrl, placeholder: placeholder)

Use callback for when the image is fetched

If you want to get more info on the fetching result, you can pass a closure as completion.

imageView.setImage(url: imageUrl) { result in
  switch result {
  case .value(let image):
    print(image)
  case .error(let error):
    print(error)
  }
}

result is an enum Result that let you know if the operation succeeded or failed. The possible error is of ImaginaryError.

Advanced

Passing option

You can also pass Option when fetching images; it allows fine grain control over the fetching process. Option defaults to no pre-processor and a displayer for ImageView.

let option = Option()
imageView.setImage(url: imageUrl, option: option)

Pre-processing

Images are fetched, decompressed and pre-processed in the background. If you want to modify, simply implement your own ImageProcessor and specify it in the Option. The pre-processing is done in the background, before the image is set into view.

public protocol ImageProcessor {
  func process(image: Image) -> Image
}

This is how you apply tint color before setting images.

let option = Option(imagePreprocessor: TintImageProcessor(tintColor: .orange))
imageView.setImage(url: imageUrl, option: option)

Imaginary provides the following built in pre-processors

  • [x] TintImageProcessor: apply tint color using color blend effect
  • [ ] ResizeImageProcessor: resize
  • [ ] RoundImageProcessor: make round corner

Displaying

Imaginary supports any View, it can be UIImageView, UIButton, MKAnnotationView, UINavigationBar, ... As you can see, the fetching is the same, the difference is the way the image is displayed. To avoid code duplication, Imaginary take advantages of Swift protocols to allow fully customisation.

You can roll out your own displayer by comforming to ImageDisplayer and specify that in Option

public protocol ImageDisplayer {
  func display(placeholder: Image, onto view: View)
  func display(image: Image, onto view: View)
}

This is how you set an image for UIButton

let option = Option(imageDisplayer: ButtonDisplayer())
button.setImage(url: imageUrl, option: option)

let option = Option(imageDisplayer: ImageDisplayer(animationOption: .transitionCurlUp))
imageView.setImage(url: imageUrl, option: option)

These are the buit in displayers. You need to supply the correct displayer for your view

  • [x] ImageDisplayer: display onto UI|NSImageView. This is the default with cross dissolve animation.
  • [x] ButtonDisplayer: display onto UI|NSButton using setImage(_ image: UIImage?, for state: UIControlState)
  • [x] ButtonBackgroundDisplayer: display onto UI|NSButton using setBackgroundImage(_ image: UIImage?, for state: UIControlState)

Downloading

Imaginary uses ImageFetcher under the hood, which has downloader and storage. You can specify your own ImageDownloader together with a modifyRequest closure, there you can change request body or add more HTTP headers.

var option = Option()
option.downloaderMaker = {
  return ImageDownloader(modifyRequest: { 
    var request = $0
    request.addValue("Bearer 123", forHTTPHeaderField: "Authorization")
    return request 
  })
}

imageView.setImage(imageUrl, option: option)

Caching

The storage defaults to Configuration.storage, but you can use your own Storage, this allows you to group saved images for particular feature. What if you want forced downloading and ignore storage? Then simply return nil. For how to configure storage, see Storage

var option = Option()
option.storageMaker = {
  return Configuration.imageStorage
}

Configuration

You can customise the overal experience with Imaginary through Configuration.

  • trackBytesDownloaded: track how many bytes have been used to download a specific image
  • trackError: track if any error occured when fetching an image.
  • imageStorage: the storage used by all fetching operations.

ImageFetcher

Imaginary uses ImageFetcher under the hood. But you can use it as a standalone component.

ImageDownloader

Its main task is to download image and perform all kinds of sanity checkings.

let downloader = ImageDownloader()
downloader.download(url: imageUrl) { result in
  // handle result
}

ImageFetcher

This knows how to fetch and cache the images. It first checks memory and disk cache to see if there's image. If there isn't it will perform network download. You can optionally ignore the cache by setting storage to nil.

let fetcher = ImageFetcher(downloader: ImageDownloader(), storage: myStorage()
fetcher.fetch(url: imageUrl) { result in
  // handle result
}

MultipleImageFetcher

It sometimes makes sense to pre download images beforehand to improve user experience. We have MultipleImageFetcher for you

let multipleFetcher = MultipleImageFetcher(fetcherMaker: {
  return ImageFetcher()
})

multipleFetcher.fetch(urls: imageUrls, each: { result in
  // handle when each image is fetched
}, completion: {
  // handle when all images are fetched
})

This is ideal for the new prefetching mode in UICollectionView

Installation

Imaginary is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Imaginary'

Imaginary is also available through Carthage. To install just write into your Cartfile:

github "hyperoslo/Imaginary"

Imaginary can also be installed manually. Just download and drop Sources folders in your project.

Author

Hyper Interaktiv AS, [email protected]

License

Imaginary is available under the MIT license. See the LICENSE file for more info.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].