[翻译] 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(添加到你的工程當中)
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:
上面的代碼需要注意:
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(处理图片缓存问题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MYSQL 数学运算符问题
- 下一篇: MongoDB: The Definit