github

kean / Preheat

  • понедельник, 27 июня 2016 г. в 03:13:03
https://github.com/kean/Preheat

Swift
Automates prefetching of content in UITableView and UICollectionView



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

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

One of the ways 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, so that you can precache 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

CocoaPods

To install Preheat add a dependency to your Podfile:

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

pod "Preheat"

Carthage

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.