Coder Social home page Coder Social logo

rd-mobile-alex / sgimagecache Goto Github PK

View Code? Open in Web Editor NEW

This project forked from seatgeek/sgimagecache

0.0 2.0 0.0 403 KB

A flexible image caching library for image rich iOS applications

License: BSD 2-Clause "Simplified" License

Objective-C 99.16% Ruby 0.84%

sgimagecache's Introduction

SGImageCache

A lightweight iOS image and data cache with built in queue management.

CocoaPods Setup

pod 'SGImageCache'

Get an image urgently

// Objective-C
[SGImageCache getImageForURL:url].then(^(UIImage *image) {
    if (image) {
        self.imageView.image = image;
    }
});
// Swift
SGImageCache.getImageForURL(url) { image in
    if image {
        self.imageView.image = image
    }
}
SGImageCache.getImageForURL(url).swiftThen({object in
    if let image = object as? UIImage {
        self.imageView.image = image
    }
    return nil
})

This will add the fetch request to fastQueue (a parellel queue). All image fetching (either from memory, disk, or remote) is performed off the main thread.

Queue a fetch for an image that you'll need later

// Objective-C
[SGImageCache slowGetImageForURL:url];
// Swift
SGImageCache.slowGetImageForURL(url)

This will add the fetch request to slowQueue (a serial queue). All image fetching (either from memory, disk, or remote) is performed off the main thread.

Adding image fetch tasks to slowQueue is useful for prefetching images for off screen content. For example if you have data for 100 table rows, but only 3 are on screen at a time, you would request the images for on screen rows from fastQueue with getImageForURL: and add the rest to slowQueue with slowGetImageForURL:.

Inform the cache that an urgent image fetch is no longer urgent

// Objective-C
[SGImageCache moveTaskToSlowQueueForURL:url];
// Swift
SGImageCache.moveTaskToSlowQueueForURL(url)

This is useful for deprioritising image fetches for content that has scrolled off screen. The content may scroll back on screen later, so you still want the fetch to happen, but it is no longer urgently required.

Perform actions on remote fetch, fail or retry

// Objective-C
SGCachePromise *promise = [SGImageCache getImageForURL:url];
promise.then(^(UIImage *image) {
  self.imageView.image = image;
});
promise.onRetry = ^{
  // Called when SGImageCache automatically retries
  // fetching the image due to a reachability change.
  [self showLoadingSpinner];
};
promise.onFail = ^(NSError *error, BOOL fatal) {
  // If the failure was fatal, SGImageCache will not
  // automatically retry (eg. from a 404)
  [self displayError:error];
};
// Swift
let promise = SGImageCache.getImageForURL(url)
promise.swiftThen({object in
  if let image = object as? UIImage {
      self.imageView.image = image
  }
  return nil
})
promise.onRetry = {
  self.showLoadingSpinner()
}
promise.onFail = { (error: NSError?, wasFatal: Bool) -> () in
  self.displayError(error)
}

This is useful for displaying states of network failure, loading spinners on reachability change, or any other functionality that might need to be notified that an image could not be fetched.

fastQueue

fastQueue is a parallel queue, used for urgently required images. The getImageForURL: method adds tasks to this queue. The maximum number of parallel tasks is managed by iOS, based on the device's number of processors, and other factors.

slowQueue

slowQueue is a serial queue, used for prefetching images that might be required later (eg for currently off screen content). The slowGetImageForURL: method adds tasks to this queue.

slowQueue is automatically suspended while fastQueue is active, to avoid consuming network bandwidth while urgent image fetches are in progress. Once all fastQueue tasks are completed slowQueue will be resumed.

Task Deduplication

If an image is requested for a URL that is already queued or in progress, SGImageCache reuses the existing task, and if necessary will move it from slowQueue to fastQueue, depending on which image fetch method was used. This ensures that there will be only one network request per URL, regardless of how many times it's been asked for.

Intelligent image releasing on memory warning

If you use SGImageView instead of UIImageView, and load the image via one of the setImageForURL: methods, off screen image views will release their image on memory warning, and subsequently restore them from cache if the image view returns to screen. This allows off screen but still existing view controllers (eg a previous controller in a nav controller's stack) to free up memory that would otherwise be unnecessarily retained, and reduce the chances of your app being terminated by iOS in limited memory situations.

Generic caching of NSData

You can use SGImageCache for caching of generic data in the form of an NSData object (eg. PDFs, JSON payloads). Just use the equivalent SGCache class method instead of the SGImageCache one:

// Objective-C
[SGCache getFileForURL:url].then(^(NSData *data) {
  // do something with data
});
// Swift
SGCache.getFileForURL(url).swiftThen({object in
  if let data = object as? NSData {
    // do something with data
  }
  return nil
})

sgimagecache's People

Contributors

sobri909 avatar staminajim avatar dbachrach avatar nod avatar mxcl avatar

Watchers

James Cloos avatar  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.