Coder Social home page Coder Social logo

preheat's Introduction

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

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.

preheat's People

Contributors

kean avatar

Watchers

James Cloos avatar Lucas Farah avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.