• Stars
    star
    342
  • Rank 123,697 (Top 3 %)
  • Language
    Swift
  • License
    MIT License
  • Created about 10 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🍁πŸ₯“ Lightweight and fast Swift library for image downloading, caching and transformations

MapleBacon

CI codecov.io Version License Platform Carthage compatible SPM

Introduction

MapleBacon is a lightweight and fast Swift library for downloading and caching images.

Example

The folder Example contains a sample projects for you to try.

Requirements

  • Swift 5.1
  • iOS 10.0+
  • Xcode 10.2+

Installation

MapleBacon is available through CocoaPods. To install add it to your Podfile:

pod "MapleBacon"

Carthage

github "JanGorman/MapleBacon"

and Swift Package Manager.

Usage

UIImageView

The most basic usage is via an extension on UIImageView. You pass it URL:

import MapleBacon

private var imageView: UIImageView!

func someFunc() {
  let url = URL(string: "…")
  imageView.setImage(with: url)
}

If you want to add a placeholder while the image is downloading you specify that like this:

func someFunc() {
  let url = URL(string: "…")
  imageView.setImage(with: url, placeholder: UIImage(named: "placeholder"))
}

If your backend returns images that are not optimised for display, it's good practice to downsample them. MapleBacon comes with support for downsampling via displayOptions:

func someFunc() {
  let url = URL(string: "…")
  imageView.setImage(with: url, displayOptions: .downsampled)
}

Image Transformers

MapleBacon allows you to apply transformations to images and have the results cached so that you app doesn't need to perform the same work over and over. To make your own transformer, create a class conforming to the ImageTransforming protocol. A transform can be anything you like, let's create one that applies a Core Image sepia filter:

private class SepiaImageTransformer: ImageTransforming {

  // The identifier is used as part of the cache key. Make sure it's something unique
  let identifier = "com.schnaub.SepiaImageTransformer"

  func transform(image: UIImage) -> UIImage? {
    let filter = CIFilter(name: "CISepiaTone")!

    let ciImage = CIImage(image: image)
    filter.setValue(ciImage, forKey: kCIInputImageKey)
    filter.setValue(0.5, forKey: kCIInputIntensityKey)

    let context = CIContext()
    guard let outputImage = filter.outputImage,
          let cgImage = context.createCGImage(outputImage, from: outputImage.extent) else {
            return image
    }

    // Return the transformed image which will be cached (or used by another transformer)
    return UIImage(cgImage: cgImage)
  }

}

You then pass this filter to MapleBacon in one of the convenience methods:

let url = URL(string: "…")
let transformer = SepiaImageTransformer()
imageView.setImage(with: url, transformer: transformer)

If you want to apply multiple transforms to an image, you can chain your transformers:

let chainedTransformer = SepiaImageTransformer()
  .appending(transformer: DifferentTransformer())
  .appending(transformer: AnotherTransformer())

Or if you prefer, using the custom >>> operator:

let chainedTransformer = SepiaImageTransformer() >>> DifferentTransformer() >>> AnotherTransformer()

(Keep in mind that if you are using Core Image it might not be optimal to chain individual transformers but rather create one transformer that applies multiple CIFilters in one pass. See the Core Image Programming Guide.)

Caching

MapleBacon will cache your images both in memory and on disk. Disk storage is automatically pruned after a week (taking into account the last access date as well) but you can control the maximum cache time yourself too:

let oneDaySeconds: TimeInterval = 60 * 60 * 24
MapleBacon.default.maxCacheAgeSeconds = oneDaySeconds

Combine

On iOS13 and above, you can use Combine to fetch images from MapleBacon

MapleBacon.shared.image(with: url)
  .receive(on: DispatchQueue.main) // Dispatch to the right queue if updating the UI
  .sink(receiveValue: { image in
    // Do something with your image
  })
  .store(in: &subscriptions) // Hold on to and dispose your subscriptions

Migrating from 5.x

There is a small migration guide in the wiki when moving from the 5.x branch to 6.x

License

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

More Repositories

1

Agrume

πŸ‹ A lemony fresh iOS image viewer written in Swift.
Swift
790
star
2

Hippolyte

HTTP Stubbing in Swift
Swift
111
star
3

Chester

Chester is a Swift GraphQL query builder.
Swift
100
star
4

Table

CLI tables in Swift
Swift
55
star
5

SwiftMessageBar

An iOS message bar, written in Swift
Swift
52
star
6

uppercrust

Convert your JSON schema files to Mantle compatible Objective-C models
Ruby
24
star
7

JGORegExpBuilder

A delightful regular expressions DSL
Objective-C
12
star
8

node-table

node.js Text Table Renderer. No longer maintained. Development has moved to https://github.com/gajus/table
CoffeeScript
10
star
9

AVContentProposal-Sample

How to use AVContentProposalViewController
Swift
10
star
10

Waxwing

iOS version migrations 🦀
Swift
7
star
11

jersey-passbook

A Java implementation of the Apple Passbook REST API running on Jersey
Java
7
star
12

Swift-Design-Patterns

Design patterns applied in Swift. Swift 1 in 2014.
Swift
6
star
13

CRDT

A toy implementation of conflict-free replicated data types in Swift
Swift
4
star
14

Bling

An Open Exchange Rates API wrapper written in Swift
Swift
4
star
15

Zoetrope

Animated gif image view with support for varying frame lengths written in Swift.
Swift
3
star
16

ClosureTask

A Closure Task for phing
PHP
3
star
17

Personal-Jira

Your personal Jira search
CoffeeScript
2
star
18

my-boxen

My boxen
Ruby
1
star
19

VerbalExpressions

Objective-C Verbal Expressions
Objective-C
1
star
20

swiftlint

JavaScript
1
star
21

SwiftlintTestRunner

Test project to execute https://github.com/JanGorman/swiftlint
Swift
1
star
22

ember-by-example

Ember.js integration with Dropwizard
JavaScript
1
star
23

ChampagneBrunch

Swift
1
star
24

HARParser

A Swift .har parser
Swift
1
star
25

Sorte

Sort a directory of mp3s into folders base on their tags
Ruby
1
star
26

SwiftMosaicLayout

A mosaic UICollectionViewLayout written in Swift
Swift
1
star
27

puppet-simpholders

SimPholders Puppet Module for Boxen
Ruby
1
star