本文共 6637 字,大约阅读时间需要 22 分钟。
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.
真的。
NSCache
.UIImageView
category to use the cache, optimized for UITableView
and UICollectionView
cell reuse.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:
上面的代码需要注意:
bounds
and contentMode
of the UIImageView
) from the memory or disk cache. Disk access is performed in background.NSURLCache
if available.UIImageView
was reused during any of the above steps.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都有一个默认的格式,你也可以自定义你自己的格式,每种格式都有它自己的唯一标示的名字。
Each image view has a default format created on demand. The default format is configured as follows:
每个 iamgeView 有一个默认的格式,这个默认的格式是这么被配置的:
bounds
of the image view.contentMode
of the the image view.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; // 1MBformat.preloadPolicy = HNKPreloadPolicyLastSession;imageView.hnk_cacheFormat = format;
The image view category will take care of registering the format in the shared cache.
iamge view 的类目会注册这个格式到 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,当往这个已经满了的磁盘缓存中写入新的图片。
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.
预加载仅支持能够进行磁盘缓存的格式。
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.
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就可以看到打印信息了。
Haneke requires iOS 7.0 or above and ARC.
iOS 6 compatibility can be achieved with very few . You can use 's that adds it by replacing NSURLSession
with .
Haneke 需要 iOS 7.0 以及 ARC。
虽然兼容 iOS 6,但基本上没有啥效果,你可以使用 's 来替换 AFNetworking 中的 NSURLSession 。
Haneke is in initial development and its public API should not be considered stable.
Haneke 是一个刚刚开始的项目,所以,它公开的 API 可能会经常变动。
转载地址:http://gyrzx.baihongyu.com/