Flyimage integrates the advantages of sdwebimage,fastimagecache,afnetworking and is a new image loading framework with high performance and simple interface.
Characteristics
Efficient
Can be more than one small map decoding and storage on the same large map, in the same screen rendering multi-graph, high efficiency;
Supports mmap memory mapping, efficient I/O operations, reduced file copy operation, and reduced memory consumption;
Support Byte Alignment byte to it, during rendering, avoid performing CA::Render::copy_image memory operation;
Simple interface
Support UIImageView , CALayer Category;
Do not consider the small image size, simple storage and reading interface;
The scheme solves two kinds of business scenarios of single large image and multiple small graphs simultaneously.
WebP efficient image compression method;
asynchronous Download support download progress block, to facilitate the implementation of customized download animation;
Popular frame comparison
Now there are two more popular picture loading frames on iOS:
SDWebImageProvides a complete set of solutions from download to rendering, while supporting the category feature, WEBP format, easy to use, but the same screen multiple small graphs quickly scrolling, the interface will be very obvious lag;
FastImageCachePath launched, very suitable for small images of efficient rendering, internal optimization of I/O read, decoding support to Byte Alignment reduce memory copy, while only one decoding, can be said to be extremely optimized. But FIC has two major drawbacks:
In order to streamline the code, from 1.2 to cancel the image download function;
Interface is very difficult to use, also need to specify FICImageFormat , each format picture size must be consistent, and each picture needs and [FICEntity binding, I just want to quickly show more than one icon ...
Based on the above analysis, if there is a picture library can combine the advantages of the two together, then how good! FlyImagebased on this idea, the new library integrates FastImageCache the optimization scheme while making the interface easier to use.
FlyImageIt is possible to draw multiple small pictures of different size in one file, storing and retrieving only one fixed, and key applying the memory mapping method to the display scheme of large picture, reducing the number of copies of memory and speeding up the reading speed. Here's how to use it:
How to use the installation
Platform:ios, ' 8.0 ' pod ' flyimage ', ' ~>1.0 '
Using Uiimageview/calayer
| 123 |
UIImageView *iconView = [[UIImageView alloc] initWithFrame:frame];[iconView setIconURL:[NSURL urlWithString:@"http://original"]];[]self.view addSubview:iconView]; |
Using Flyimagecache
| 123456789 |
// 通过Key获取单张图片[[FlyImageCache sharedInstance] asyncGetImageWithKey:keycompleted:^(NSString *key, UIImage *image) { imageView.image = image;}];// 删除一张图片[[FlyImageCache sharedInstance] removeImageWithKey:key];// 清除所有图片[[FlyImageCache sharedInstance] purge]; |
Using Flyimageiconcache
Add a small picture [[flyimageiconcache sharedinstance] addimagewithkey:key size:drawsize drawingblock:^ (CGContextRef context, cgrect contextbounds) { // Manual Drawing UIImage *image = [UIImage imagewithname:@ "ImageName"]; uigraphicspushcontext (context); [image drawInRect:contextBounds]; Uigraphicspopcontext (); } completed:nil]; // get a small picture [[FlyImageCache sharedinstance] asyncgetimagewithkey:key completed:^ (nsstring *key, uiimage *image) { imageView.image = image;}];Performance memory
Test Engineering: Flyimageview/device:iphone6 Plus, continuous display of multiple large graphs in scrolling list, Flyimage does not increase memory of image IO
| Memory |
Flyimage |
Sdwebimage |
UIKit |
| All HEAP Allocations |
2~7m |
2~4m |
2~5m |
| All Anonymous VMS |
17~30m |
310M |
17~30m |
Fps
Test Engineering: Flyimageiconview/device:iphone6 Plus, render 170 small graphs on the same screen, flyimage a smooth browsing experience
| Flyimage |
Sdwebimage |
UIKit |
| 58~60fps |
6~7fps |
6~7fps |
Multi-image on the same screen
Class diagram
Flyimagedatafile encapsulated mmap Operations, providing efficient I/O file operations, support read, write, dynamically expand the file length of the function.
Flyimagedatafilemanager Responsible FlyImageDataFIle for additions, deletions, and lookups. Users cannot instantiate directly FlyImageDataFIle , they need to do this through the manager, and they can get the number of files and occupy space under the current folder.
Flyimagedecoder decodes the memory data and generates the UIImage object. The WebP conversion of the format is done in that class.
Flyimageencoder is a FlyImageIconCache class service that draws pictures onto the canvas to generate bitmap formatting.
Flyimagecache is responsible for image additions, deletions, and lookups. Each picture corresponds to one key , and this information is stored in a meta file. When the class is instantiated, it is automatically created or automatically fetched meta , and you can specify a different meta file path. In the actual use of the process, the app will be provided to clear the current cache operation, but also want to keep some necessary pictures, such as the current user's avatar and unpublished draft pictures, for this demand, FlyImageCache provides a convenient interface - (void)protectFileWithKey:(NSString*)key; and - (void)unProtectFileWithKey:(NSString*)key; operation, in protect The picture of the state is purge not cleared even when the operation is performed.
Flyimageiconcache is responsible for adding, deleting, replacing, and locating small images. FlyImageCacheis basically consistent with the interface, except that the class maintains only one FlyImageDataFIle case, and all small images are stored in the file after decoding. Of course you can also create multiple instances and put small pictures that you often use together in one FlyImageDataFIle .
Flyimagedownloader is responsible for downloading images, note that this class is not responsible for storage. After initiating a download request, you get an FlyImageDownloadHandlerId identifier of type, and if the picture is moved out of the current display area, you can - (void)cancelDownloadHandler:(FlyImageDownloadHandlerId*)handlerId; save resources by calling remove the download request.
Uiimageview+flyimagecache, calayer+flyimagecache UIImageView provides a convenient classification interface '.
-(void) Setimageurl: (nsurl*) URL;
Uiimageview+flyimageiconcache, calayer+flyimageiconcache CALayer provides a convenient classification interface '.
-(void) Seticonurl: (nsurl*) URL;
Source
Https://github.com/northwind/FlyImage, you are welcome to test and give feedback, thank you.
Reference
Fastimagecache
Sdwebimage
Careful analysis of mmap: What is the reason for how to use
iOS picture loading new frame-flyimage