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

[翻译] Haneke(处理图片缓存问题)的更多相关文章

  1. 006 [翻译] Haneke(一个Swfit iOS缓存类)

    Github项目地址:https://github.com/Haneke/HanekeSwift Haneke是一个用swift写成的轻量级iOS类,以简单好用著称(design-decisions- ...

  2. HTML5之appcache语法理解/HTML5应用程序缓存/manifest缓存文件官方用法翻译

    习惯性的贴几个参考链接: W3School-HTML 5 应用程序缓存 官方 MDN window.applicationCache 接口文档 官方 MDN 用法示例 看所有的教程不如直接看最原始的官 ...

  3. Android Bitmap 全面解析(二)加载多张图片的缓存处理

    一般少量图片是很少出现OOM异常的,除非单张图片过~大~ 那么就可以用教程一里面的方法了通常应用场景是listview列表加载多张图片,为了提高效率一般要缓存一部分图片,这样方便再次查看时能快速显示~ ...

  4. Django—— 缓存框架

    译者注:1.无用的,吹嘘的说辞不翻译:2.意译,很多地方不准确. 动态网站最为重要的一点就是好,网页是动态的.每一次用户请求页面,网站就要进行各种计算——从数据库查询,到render模板,到各种逻辑运 ...

  5. 读懂操作系统之虚拟内存TLB与缓存(cache)关系篇(四)

    前言 前面我们讲到通过TLB缓存页表加快地址翻译,通过上一节缓存原理的讲解为本节做铺垫引入TLB和缓存的关系,同时我们来完整梳理下从CPU产生虚拟地址最终映射为物理地址获取数据的整个过程是怎样的,若有 ...

  6. git和github使用方式

    git 和github github是远程管理代码的服务器的名称 git代码管理系统 (git既然是一个系统,所以说git也有一些命令) git操作过程 首先在本地建立一个仓库,用来把代码提交到git ...

  7. iOS 开发--开源图片处理圆角

    概述 开源项目名称:HYBImageCliped 当前版本:2.0.0 项目用途:可给任意继承UIView的控件添加任意多个圆角.可根据颜色生成图片且可带任意个圆角.给UIButton设置不同状态下的 ...

  8. Retina屏下1px border

    layout tltle tags post ios7下移动web开发的几个坑 webapp 1.Retina屏下1px border 由于高清屏的特性,1px是由2×2个像素点来渲染,那么我们样式上 ...

  9. OpenRisc-39-ORPSoC,or1200的memory hierarchy整体分析

    引言 前面我们简单分析了ORPSoC的整体结构,or1200_top的整体结构,or1200_cpu的整体结构. 并对ORPSoC的启动过程,ORPSoC的debug子系统,clock子系统进行了介绍 ...

随机推荐

  1. 使用Kafka、Elasticsearch、Grafana搭建业务监控系统(三)Elasticsearch

    https://blog.csdn.net/tonywu1992/article/details/83576863

  2. 【LOJ】 #2665. 「NOI2013」树的计数

    题解 我们统计深度对于bfs序统计,树结构出现分歧的地方必然是BFS序的最后一段,这个最后一段同时还得是dfs序上连续的一段 如果不是bfs序的最后一段,那么必然下一层会有节点,如果树结构分歧了,那么 ...

  3. pip-django-cms

    pip install django-el_pagination pip install django-ckeditor

  4. Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) E - Aquarium decoration 贪心 + 平衡树

    E - Aquarium decoration 枚举两个人都喜欢的个数,就能得到单个喜欢的个数,然后用平衡树维护前k大的和. #include<bits/stdc++.h> #define ...

  5. Django实战(11):修改Model类

    我们已经实现了卖方的产品维护界面,根据最初的需求,还要为买方实现一个目录页:买方通过这个界面浏览产品并可以加入购物车.通过进一步需求调研,了解到产品有一个“上架时间”,在这个时间之后的产品才能被买方看 ...

  6. 8-16 不无聊序列 non-boring sequences uva1608

    题意: 如果一个序列的任意连续子序列中至少有一个只出现一次的元素  则称这个序列是 不无聊序列  输入一个n个元素的序列a   判断是不是不无聊序列 一开始想当然  以为只要 2位的子序列都满足即可 ...

  7. List元素为泛型时的注意事项

    最近的项目赶得非常紧,这节奏跟最近的天气一点也不搭调. 编码的过程,遇到一个关于List的小问题. 在调用List.add(E e)的时候范了一个小毛病,很自然地认为list中存储的是 E  对象的另 ...

  8. TRUNCATE can't with condition

    No, TRUNCATE is all or nothing. You can do a DELETE FROM <table> WHERE <conditions> but ...

  9. To 初识Java的小菜菜们 嘻嘻~

    一.Java Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言.Java 技术具有卓越的通用性.高效性.平台移植性和安全性,广泛应用于PC.数据中心.游戏控制台.科学超级计算机.移动电话和 ...

  10. PAGELATCH_EX Contention on 2:1:103

    This blog post is meant to help people troubleshoot page latch contention on 2:1:103. If that’s what ...