Giter Site home page Giter Site logo

wkimagecache's Introduction

WKImageCache

UPDATE: WKImageCache was originally written for watchOS 1.0. I'm no longer using it in any project, but will still accept pull requests for improvements.

For images you create in your WatchKit extension but use frequently, Apple recommends caching those images on the device and refer to them by name. There are some basic functions like addCachedImage:name: or removeCachedImageWithName: to manage this. This is nice, but you have to take care of removing old images to free up a full cache (about 5MB per app) all by yourself.

The WKImageCache doesn't replace that cache, it's merely a simple wrapper that tries to solve this problem. If the user wants to add a new image and the cache is full, the WKImageCache automatically removes previous images based on currently two strategies, FIFO and LRU.

Take a look at the Example project to see how to use it.

Requirements

WKImageCache requires iOS8.2 or above.

Installation

Copy the WKImageCache.swift file to your Swift project and add it to your target. After that you need to (if not already exists) create a bridging header by choosing File > New > File > iOS > Source > Header File and name it projectname-bridging-header.h. Then add the following line and you're good to go:

#import <CommonCrypto/CommonCrypto.h>

Usage

WKImageCache provides extensions for three interface types that currently support displaying images: WKInterfaceImage, WKInterfaceButton and WKInterfaceGroup.

For example, if you want to display an image (using the WKInterfaceImage) and cache it for later usage, you would use the following method:

interfaceImage.setCachedImage(image)

The image will be compressed, transmitted and added to the cache. If the image is already chached, this version will be used. There are additional (optional) parameters that you can use for adjusting the behaviour:

interfaceImage.setCachedImage(image, compression: 0.6, cacheType: .LRU)
  • The compression (0.6 is default) is applied to the image before transmitting it. The lower the compression value, the faster the transmission.
  • The default cache type is FIFO, which means that the oldest image in the cache will be removed if there's no space left. Alternatively, you can use LRU, which discards the least recently used images first.

If the image is available as NSData, use this function:

interfaceImage.setCachedImageData(data)

The functions for WKInterfaceButton and WKInterfaceGroup are called

  • setCachedBackgroundImage(image : UIImage)
  • setCachedBackgroundImageData(image : UIImage)

and work equally.

License

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

Recent Changes

The release notes can be found here.

TODO

  • Code Documentation

wkimagecache's People

Contributors

mkoehnke avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

wkimagecache's Issues

Add a way to synchronize the cache data

WKImageCache stores it's data using the NSUserDefaults. The data should be synchronized automatically when the user e.g. brings the app in the background.

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.