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

简介:

Haneke

https://github.com/hpique/Haneke

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 可能会经常变动。

目录
相关文章
|
4月前
|
存储 缓存 安全
基于iOS平台的高效图片缓存策略实现
【4月更文挑战第22天】 在移动应用开发中,图片资源的加载与缓存是影响用户体验的重要因素之一。尤其对于iOS平台,由于设备存储空间的限制以及用户对流畅性的高要求,设计一种合理的图片缓存策略显得尤为关键。本文将探讨在iOS环境下,如何通过使用先进的图片缓存技术,包括内存缓存、磁盘缓存以及网络请求的优化,来提高应用的性能和响应速度。我们将重点分析多级缓存机制的设计与实现,并对可能出现的问题及其解决方案进行讨论。
|
4月前
|
存储 缓存 编解码
实现iOS平台的高效图片缓存策略
【4月更文挑战第23天】在移动应用开发领域,尤其是图像处理密集型的iOS应用中,高效的图片缓存策略对于提升用户体验和节省系统资源至关重要。本文将探讨一种针对iOS平台设计的图片缓存方案,该方案通过结合内存缓存与磁盘缓存的多层次结构,旨在优化图片加载性能并降低内存占用。我们将深入分析其设计理念、核心组件以及在实际场景中的应用效果,同时对比其他常见缓存技术的优势与局限。
|
Java Android开发
ImagePicker在安卓上对图片的应用一例
偷得浮生半日闲,早上在逛StackOverflow的时候发现有人求助,提到了一个不怎么火的ImagePicker库:
134 0
|
Web App开发 缓存 网络协议
【翻译工具】如何复活谷歌翻译(网页翻译)
【翻译工具】如何复活谷歌翻译(网页翻译)
911 0
|
Java Android开发
Android 小巧技-不用图片框架,实现加载类似微博超长图片的手法
众所周知,人生是一个漫长的流程,不断克服困难,不断反思前进的过程。在这个过程中会产生很多对于人生的质疑和思考,于是我决定将自己的思考,经验和故事全部分享出来,以此寻找共鸣!!!
535 0
|
缓存 Swift iOS开发
iOS开发swift版异步加载网络图片(带缓存和缺省图片)
iOS开发swift版异步加载网络图片(带缓存和缺省图片)
466 0
|
缓存 编解码 JavaScript
Flutter 图片解码与缓存管理研究
图片解码和缓存管理是渲染引擎的一个重要模块,这是因为图片解码的耗时很长,特别是对于设计为跨平台的通用渲染引擎来说,依赖于CPU来做图片解码,会消耗大量的CPU时间,并且图片解码后占用的内存很大,一张 1024x1024 分辨率的图片解码后就需要 4M 内存(除非硬件支持实时生成无损压缩格式纹理,通常这也不在通用渲染引擎的考虑范围之内)。所以一个设计良好的图片解码和缓存管理模块需要平衡很多不同的因素
1066 0
|
缓存 BI
UWP开发中两种网络图片缓存方法
原文:UWP开发中两种网络图片缓存方法   通常情况下,我们的程序需要从服务器读取图片,但如果需要不止一次读取某一张图片的话,就需要做本地缓存了,这样既为用户省一点流量,又能显得你的APP很快。   假如你已经知道了某一张图片的地址,那么第一件事就是要把这张图片下载下来;当然如果是一次性读取的话,可以直接把图片地址给Image控件或者给Bitmapimage对象(实际上这二者是没有去别的),但这无法存到本地,只作为显示用;但是我们要做的是保存到本地,这样肯定是不行的。
1090 0