Skip to content

Migration Guide Version 4 β†’ Version 5

Jan Gorman edited this page Oct 22, 2017 · 1 revision

Migrating existing code

Under the hood everything is new in this release. There are some small changes to the API. Overall usage has been streamlined catering for the 80% use case.

Basic Usage

There are a few small changes to the API but for the most basic call on UIImageView only the withUrl keyword changed to url.

Old:

imageView.setImage(withUrl: url)

New:

imageView.setImage(with: url)

Adding a placeholder hasn't changed, so:

New:

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

The crossFadePlaceholder and cacheScaled parameters are both out in the latest release. There's no more "special" image scaling mode, instead MapleBacon relies entirely on what UIImageView does out of the box.

Progress

There is now a new, per image, optional progress handler.

New:

imageView.setImage(with: url, progress: { progress, total in
  // e.g. we have a progress bar we want to update
  self.downloadProgressView.progress = Float(progress) / Float(total)
})

Completion

As before there's also an optional completion handler that's called when the download finished:

New:

imageView.setImage(with: url) { image in
  // Do something with the image, or hide a progress indicator, update network indicator status, etc …
}

Misc

Memory pressure

In the old version, if the app came under memory pressure, and you wanted to clear cached images you manually needed to call clearMemoryStorage() on the shared instance. The new version takes care of this by itself.

Direct access

If you want to download images "manually" you can access the shared MapleBacon instance directly.

Old:

ImageManager.sharedManager.downloadImage(atUrl: imageUrl, storage: storage) { imageInstance, error in
  // Do something with the image
}

New:

MapleBacon.shared.image(with: url, progress: { progress, total in
  // Do something with progress
}, completion: { image in 
  // Do something with the image
})

You can also initialise your own MapleBacon instance with a custom Cache and custom Downloader instance (or rely on either of the defaults).

Adjusting the max cache time

The default is 1 week for images. Note though that the new version also takes into account the last access time of a file, not just it's age on disk which should cut down on network traffic.

Old:

let maxAgeOneDay: TimeInterval = 60 * 60 * 24
DiskStorage.sharedStorage.maxAge = maxAgeOneDay

New:

let oneDaySeconds: TimeInterval = 60 * 60 * 24
Cache.shared.maxCacheAgeSeconds = oneDaySeconds