zoukankan      html  css  js  c++  java
  • 关于ios异步加载图片的几个开源项目

    一、HjCache 

    原文:http://www.markj.net/hjcache-iphone-image-cache/

    获取 HJCache:

    HJCache is up on github here: https://github.com/markofjohnson/HJCache


    HjCache 是iOS上的一个开源的库,用于异步加载网络图片并在滚动中的tables中显示,同时还支持将图片缓存在本地。HjCache有如下的特性:

    1、本地缓存让程序在下次使用时,能够快速地显示。

    2、可以自定缓存的大小。

    3、缓存中的图片在app中的任意地方都可以使用。

    4、当下载被中断时,未完成的图片的缓存并不会被打乱。

    5、当界面在跳转时,允许未完成下载的图片继续下载,并完成缓存。

    6、必要时,能够对所要用到的图片进行预加载。

    上面只是HJCache 的所有特性中的以下部分,HJCache可以做的还有很多

    HJCache在设计时想要达到的目的是:

    1、让异步加载变得容易。

    2、能够轻松地使用本地缓存。

    3、在Cocoa体系中,让内存管理变得轻松。

    4、支持典型的浏览图片的方式。

    5、让library能够应对更多的数据类型,不单单之时图片资源。

    如何使用HJCache:

    首先在程序初始化的时候创建一个HJObjManager类的实例,如果需要用到本地缓存,则还需要一个HJMOFileCache类的实例:

     

    objMan = [[HJObjManager alloc] init];  

    //if you are using for full screen images, you'll need a smaller memory cache:  

    //objMan = [[HJObjManager alloc] initWithLoadingBufferSize:2 memCacheSize:2]];   

    NSString* cacheDirectory = [NSHomeDirectory() stringByAppendingString:@"/Library/Caches/imgcache/flickr/"] ;   

    HJMOFileCache* fileCache = [[[HJMOFileCache alloc] initWithRootPath:cacheDirectory] autorelease];  

    objMan.fileCache = fileCache;  


    HJManagedImageV 是UIView的子类,用于显示托管于Managed的Images(从URL异步加载、本地缓存中的或其他地方的image)。我们能在任何地方像使用UIImageVIew那样使用HJManagedImageV,在IB也是如此。

    下面例子显示如何显示被托管的images,我们设置其URl,然后托管给Manager:

     

    managedImage.url = imageUrlForCurrentRow;  

    [self.objectManager manage:managedImage];  

    //NB, the library wants to be used from the main thread, so if you're not in the main thread, use:  

    //[self.objectManager performSelectorOnMainThread:@selector(manage:) withObject:managedImage waitUntilDone:YES];  


    这就是我们要做的全部,Manager会track 那些使用了被托管的UImages的views,我们能像普通 UIVIew那样对被托管了的images进行内存管理,当images不再在屏幕中显示时,manager会自动进行处理,或者清理内存或在内存中缓存 指定数量的images或者进行本地缓存工作。

    需要注意的是:

    HJCache真正的使用者是HJManagedImageV,HJimagedImageV在显示images和在于缓存交互的时候 并没有做太多的事情,以为HJObjManager类接管了所有繁重的、单调的工作。如果HJManagedImageV的表现不符合你的要求,那就放开 手脚去继承它、修改它,要知道HJCache不单单只支持image。

    里面的Demo只是一个简单的示范,HJChahe能够用更加绚丽的UI中,能够提供更加强大的功能!!

     二、EGOImageLoading

    EGOImageLoading是用的比较多的一个第三方图片异步加载类,可以在git上找到并下载它,链接如下。另外提一下,广为人知的下拉刷新EGORefreshTableHeaderView也是就是这个人写的。
    https://github.com/enormego/EGOImageLoading
    (下载后运行demo程序XCode会提示找不到EGOCache.h头文件,可以在这个地方下载

    https://github.com/enormego/EGOCache)

    使用方法可以参照里面的demo程序,很简单,只要把ImageUrl告诉它,剩下的就什么都不用管了,它会帮你异步加载,还会做缓存处理

        // 设置默认占位图 片    myEgoimageView.placeholderImage = [UIImage imageNamed:@"placeholder.png"];    // 告 诉它图片的url地址, done    myEgoimageView.imageURL = [NSURLURLWithString:@" http://www.baidu.com/img/baidu_sylogo1.gif"];  三、SDWebImage

    SDWebImage托管在github上。https://github.com/rs/SDWebImage

    这个类库提供一个UIImageView类别以支持加载来自网络的远程图片。具有缓存管理、异步下载、同一个URL下载次数控制和优化等特征。
    使用示范的代码:

    UITableView使用UIImageView+WebCache类(基本应用,UIImageView的一个category)

    前提#import导入UIImageView+WebCache.h文件,然后在tableview的cellForRowAtIndexPath:方法下:

    - (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath {
             staticNSString *MyIdentifier =  @" MyIdentifier ";
             UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:MyIdentifier];
             if(cell == nil) {
                    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault                                reuseIdentifier:MyIdentifier] autorelease];
              }
             // Here we use the new provided setImageWithURL: method to load the web image          [cell.imageView  setImageWithURL:[NSURL URLWithString: @" http://www.baidu.com/img/baidu_sylogo1.gif "         placeholderImage:[UIImage imageNamed: @" placeholder.png "]];
             cell.textLabel.text =  @" My Text ";
             return cell;

    基本代码:[imageView setImageWithURL:[NSURL URLWithString:@"http://www.baidu.com/img/baidu_sylogo1.gif"]];

    使用SDWebImageManager类:可以进行一些异步加载的工作。

    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    UIImage *cachedImage = [manager imageWithURL:url];  // 将需要缓存的图片加载进来
    if(cachedImage) {
             // 如果Cache命中,则直接利用缓存的图片进行有关操作
             // Use the cached image immediatly
    else{
             // 如果Cache没有命中,则去下载指定网络位置的图片,并且给出一个委托方法
             // Start an async download
             
    [manager downloadWithURL:url  delegate:self];

    当然你的类要实现SDWebImageManagerDelegate协议,并且要实现协议的webImageManager:didFinishWithImage:方法。

    // 当下载完成后,调用回调方法,使下载的图片显示
    - ( void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image {
             // Do something with the downloaded image
    }

    独立的异步图像下载
    可能会单独用到异步图片下载,则一定要用downloaderWithURL:delegate:来建立一个SDWebImageDownloader实例。 

    downloader = [SDWebImageDownloader downloaderWithURL:url  delegate:self];

    这样SDWebImageDownloaderDelegate协议的方法imageDownloader:didFinishWithImage:被调用时下载会立即开始并完成。
    独立的异步图像缓存SDImageCache类提供一个创建空缓存的实例,并用方法imageForKey:来寻找当前缓存。

    UIImage *myCachedImage = [[SDImageCache sharedImageCache] imageFromKey:myCacheKey];

    存储一个图像到缓存是使用方法storeImage: forKey: 

    [[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey];

    默认情况下,图像将被存储在内存缓存和磁盘缓存中。如果仅仅是想内存缓存中,要使用storeImage:forKey:toDisk:方法的第三个参数带一负值来替代。

  • 相关阅读:
    【转】构建高性能WEB站点之 吞吐率、吞吐量、TPS、性能测试
    【计算机二级Java语言】卷015
    【计算机二级Java语言】卷014
    【计算机二级Java语言】卷013
    【计算机二级Java语言】卷012
    【计算机二级Java语言】卷011
    【计算机二级Java语言】卷010
    【计算机二级Java语言】卷009
    【计算机二级Java语言】卷008
    【计算机二级Java语言】卷007
  • 原文地址:https://www.cnblogs.com/lisa090818/p/3435075.html
Copyright © 2011-2022 走看看