[翻译] Haneke(处理图片缓存问题)-LMLPHP

Haneke

https://github.com/hpique/Haneke

[翻译] Haneke(处理图片缓存问题)-LMLPHP

A lightweight zero-config image cache for iOS.

轻量级0配置图片缓存。

Haneke resizes images and caches the result on memory and disk. Everything is done in background, allowing for fast, responsive scrolling. Asking Haneke to load, resize, cache and display an appropriately sized image is as simple as:

Haneke 重新设置图片尺寸并将图片缓存到内存和磁盘上。所有这些事都是在后台完成的,为了快速而流畅的 scrolling 。调用如此简单:

[imageView hnk_setImageFromURL:url];

Really.

真的。

Features(特性)

  • First-level memory cache using NSCache.
  • Second-level LRU disk cache using the file system.
  • Zero-config UIImageView category to use the cache, optimized for UITableView and UICollectionViewcell reuse.
  • Asynchronous and synchronous image retrieval.
  • Background image resizing and file reading.
  • Image decompression.
  • Custom image transformations before and after resizing.
  • Thread-safe.
  • Automatic cache eviction on memory warnings or disk capacity reached.
  • Preloading images from the disk cache into memory on startup.
  • NSCache 第一级别的缓存
  • 第二级别的文件系统缓存
  • 0配置的 UIImageView 类目来使用缓存,对 UITableView 以及 UICollectionViewCell 重用进行优化
  • 异步以及同步的图片恢复技术
  • 在后台进行图片尺寸的设置以及文件读写
  • 图片解压缩
  • 在重新设置图片尺寸之间可以自定义图片的变换形式
  • 线程安全
  • 自动清除内存警告或者磁盘容量溢出
  • 在内存启用的时候就已经从磁盘上预加载了图片

Add Haneke to your project(添加到你的工程当中)

  1. Add the Haneke folder to your project.
  2. Profit!
  3. 将 Haneke 文件夹添加到你的工程当中。
  4. 尽情享用吧!

UIImageView category(UIImageView 类目)

Haneke provides convenience methods for UIImageView with optimizations for UITableView and UICollectionView cell reuse. Images will be resized appropriately and cached in a shared cache.

Haneke 给 UIImageView 提供了便利的方法,用来给 UITableView 以及 UICollectionVIew 进行重用。图片会被适当的重新设置尺寸并缓存到单例缓存当中。

// Setting a remote image
[imageView hnk_setImageFromURL:url]; // Setting a local image
[imageView hnk_setImageFromFile:path]; // Setting an image manually. Requires you to provide a key.
[imageView hnk_setImage:image withKey:key];

The above lines take care of:

上面的代码需要注意:

  1. If cached, retrieving an appropriately sized image (based on the bounds and contentMode of the UIImageView) from the memory or disk cache. Disk access is performed in background.
  2. If not cached, loading the original image from web/disk/memory and producing an appropriately sized image, both in background. Remote images will be retrieved from the shared NSURLCache if available.
  3. Setting the image and animating the change if appropriate.
  4. Or doing nothing if the UIImageView was reused during any of the above steps.
  5. Caching the resulting image.
  6. If needed, evicting the least recently used images in the cache.
  7. 如果已经缓存了,就会从内存或者磁盘缓存文件中恢复出设置过尺寸的图片(基于 bounds 以及 UIIamgeView 的 contentMode)。磁盘操作都在后台进行。
  8. 如果还没有进行缓存,从web/磁盘/内存中加载原始的图片并创建出便利的图片尺寸,都是在后台进行的。web上的图片将会被恢复加载,在这个当你NSURLCache中,如果存在的话。
  9. 如果便利则会设置图片以及动态改变。
  10. 如果重用了,上面的都不是执行。
  11. 缓存重用的图片。
  12. 如果有需求,移除掉缓存中使用最少的图片。

Cache formats(缓存格式)

The cache behavior can be customized by defining cache formats. Each image view has a default format and you can also define your own formats. A format is uniquely identified by its name.

你是可以定制缓存的行为的。每张图片View都有一个默认的格式,你也可以自定义你自己的格式,每种格式都有它自己的唯一标示的名字。

UIImageView format(UIImageView 的格式)

Each image view has a default format created on demand. The default format is configured as follows:

每个 iamgeView 有一个默认的格式,这个默认的格式是这么被配置的:

  • Size matches the bounds of the image view.
  • Images will be scaled based on the contentMode of the the image view.
  • Images can be upscaled if they're smaller than the image view.
  • High compression quality.
  • No preloading.
  • Up to 10MB of disk cache.

Modifying this default format is discouraged. Instead, you can set your own custom format like this:

不要修改默认的图片格式。相对的,你可以设置你自己的图片格式:

HNKCacheFormat *format = [[HNKCacheFormat alloc] initWithName:@"thumbnail"];
format.size = CGSizeMake(320, 240);
format.scaleMode = HNKScaleModeAspectFill;
format.compressionQuality = 0.5;
format.diskCapacity = 1 * 1024 * 1024; // 1MB
format.preloadPolicy = HNKPreloadPolicyLastSession;
imageView.hnk_cacheFormat = format;

The image view category will take care of registering the format in the shared cache.

iamge view 的类目会注册这个格式到 cache 的单例中。

Disk cache(磁盘缓存)

A format can have disk cache by setting the diskCapacity property with a value greater than 0. Haneke will take care of evicting the least recently used images of the format from the disk cache when the disk capacity is surpassed.

磁盘缓存 diskCapacity 是可以设置的。Haneke 会自动移除掉使用得最少的 image,当往这个已经满了的磁盘缓存中写入新的图片。

Preload policy(预加载策略)

When registering a format, Haneke will load none, some or all images cached on disk into the memory cache based on the preload policy of the format. The available preload policies are:

当注册了一种格式后,Haneke会将磁盘上缓存的图片加载到内存的缓存当中,基于这个预加载的策略。提供给你的预加载策略如下:

  • HNKPreloadPolicyNone: No images will be preloaded.
  • HNKPreloadPolicyLastSession: Only images from the last session will be preloaded.
  • HNKPreloadPolicyAll: All images will be preloaded.

If an image of the corresponding format is requested before preloading finishes, Haneke will cancel preloading to give priority to the request. To make the most of this feature it's recommended to register formats on startup.

如果此时有一张图片开始请求了,在预加载完成之前发生的,Haneke 会取消预加载而响应这个请求。为了最大限度的使用这个特性,建议你在程序开始运行的时候就开始注册。

Preloading only applies to formats that have disk cache.

预加载仅支持能够进行磁盘缓存的格式。

Pre and post resize blocks

Formats can have blocks that will be called before and after the original image is resized: preResizeBlock and postResizeBlock respectively. Both receive a key and the image up to the corresponding stage. For example:

format.postResizeBlock = ^UIImage* (NSString *key, UIImage *image) {
UIImage *roundedImage = [image imageByRoundingCorners];
return roundedImage;
};

These blocks will be called only if the requested image is not found in the cache. They will be executed in background when using the image view category or the asynchronous methods of the cache directly.

Logging(debug)

Haneke provides useful logging that is turned off by default. You can see it in action in the demo.

Haneke 提供好用的打印信息,默认是关闭了的。你可以在demo中看到效果。

To turn logging on you must set the preprocessor macro HANEKE_DEBUG to 1. The recommended way to do this is by adding HANEKE_DEBUG=1 to the Preprocessor Macros build setting. If you included Haneke directly, add it to your project target. If you are using CocoaPods, add it to the Pods-Haneke target of the Pods project.

你把预处理宏 HANEKE_DEBUG 设置成1就可以看到打印信息了。

Requirements(环境要求)

Haneke requires iOS 7.0 or above and ARC.

iOS 6 compatibility can be achieved with very few changes. You can use @shkutkov's fork that adds it by replacing NSURLSession with AFNetworking.

Haneke 需要 iOS 7.0 以及 ARC。

虽然兼容 iOS 6,但基本上没有啥效果,你可以使用  @shkutkov's fork 来替换 AFNetworking 中的 NSURLSession 。

Roadmap(任重而道远)

Haneke is in initial development and its public API should not be considered stable.

Haneke 是一个刚刚开始的项目,所以,它公开的 API 可能会经常变动。

05-07 14:56