Skip to content

fe9lix/Preheat

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Automates preheating (prefetching) of content in UITableView and UICollectionView.

This library is similar to UITableViewDataSourcePrefetching and UICollectionViewDataSourcePrefetching added in iOS 10

One way to use Preheat is to improve user experience in applications that display collections of images. Preheat allows you to detect which cells are soon going to appear on the display, and prefetch images for those cells. You can use Preheat with any image loading library, including Nuke which it was designed for.

The idea of automating preheating was inspired by Apple’s Photos framework example app.

Getting Started

Usage

Here is an example of how you might implement preheating in your application using Preheat and Nuke:

class PreheatDemoViewController: UICollectionViewController, PreheatControllerDelegate {
    var preheatController: PreheatController<UICollectionView>!

    override func viewDidLoad() {
        super.viewDidLoad()

        preheatController = PreheatController(view: collectionView!)
        preheatController.handler = { [weak self] in
            self?.preheatWindowChanged(addedIndexPaths: $0, removedIndexPaths: $1)
        }
    }

    func preheatWindowChanged(addedIndexPaths added: [NSIndexPath], removedIndexPaths removed: [NSIndexPath]) {
        func requestsForIndexPaths(indexPaths: [NSIndexPath]) -> [ImageRequest] {
            return indexPaths.map { ImageRequest(photos[$0.row].URL) }
        }
        Nuke.startPreheatingImages(requestsForIndexPaths(added))
        Nuke.stopPreheatingImages(requestsForIndexPaths(removed))
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        preheatController.enabled = true
    }

    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)

        // When you disable preheat controller it removes all preheating 
        // index paths and calls its handler
        preheatController.enabled = false
    }
}

Requirements

  • iOS 8.0+, tvOS 9.0+
  • Xcode 7.3+, Swift 2.2+

Installation

To install Preheat add a dependency to your Podfile:

# source 'https://github.com/CocoaPods/Specs.git'
# use_frameworks!
# platform :ios, "8.0"

pod "Preheat"

To install Preheat add a dependency to your Cartfile:

github "kean/Preheat"

Import

Import installed modules in your source files

import Preheat

Contacts

License

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

About

Automates prefetching of content in UITableView and UICollectionView

Resources

License

Stars

1 star

Watchers

0 watching

Forks

Packages

 
 
 

Contributors

Languages

  • Swift 92.0%
  • Ruby 8.0%