SDWebImage的简单使用

一、前言

  第一次使用SDWebImage是在自己做《康复医生》的时候。记得那时有一个表格要显示所有的用户头像。不使用它的时候每次加载该table都会很卡顿,后来使用后发现不卡顿了。瞬间感觉这个世界好有爱。

二、安装

  首先,SDWebImage的git地址是:https://github.com/rs/SDWebImage。我们可以直接到这里进行下载,然后添加到自己的项目中去。

三、使用场景(前提是已经导入了SDWebImage这个库)

1、场景一、加载图片

    使用SDWebImage可以去加载远程图片,而且还会缓存图片,下次请求会看一下是否已经存在于缓存中,如果是的话直接取本地缓存,如果不是的话则重新请求。使用方法很简单,在需要使用该场景的类中导入

#import "UIImageView+WebCache.h"

然后调用:

- (void)sd_setImageWithPreviousCachedImageWithURL:(NSURL *)url andPlaceholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock;

即可。

  我们还可以在UIImageView+WebCache.h中看到其他的方法,和上边的方法类似,读者自行查看即可。

2、场景二、使用它做本地缓存

  很多时候我们可能拍照得到的一张图片要多个地方使用,那么我们就希望可以把这张图片放到缓存里面,然后每次用这张图片的时候就去通过特定的方式取即可。SDWebImage就有这样的一个类:SDImageCache。该类完美地帮助了我们解决了这个问题。

  使用的时候,我们首先要在使用的类里面做导入:

#import "SDImageCache.h"

然后就可以进行相关的操作了。让我们直接看看SDImageCache.h文件:

@property (assign, nonatomic) NSUInteger maxMemoryCost;

/**
 * The maximum length of time to keep an image in the cache, in seconds
 */
@property (assign, nonatomic) NSInteger maxCacheAge;

/**
 * The maximum size of the cache, in bytes.
 */
@property (assign, nonatomic) NSUInteger maxCacheSize;

/**
 * Returns global shared cache instance
 *
 * @return SDImageCache global instance
 */
+ (SDImageCache *)sharedImageCache;

/**
 * Init a new cache store with a specific namespace
 *
 * @param ns The namespace to use for this cache store
 */
- (id)initWithNamespace:(NSString *)ns;

/**
 * Add a read-only cache path to search for images pre-cached by SDImageCache
 * Useful if you want to bundle pre-loaded images with your app
 *
 * @param path The path to use for this read-only cache path
 */
- (void)addReadOnlyCachePath:(NSString *)path;

/**
 * Store an image into memory and disk cache at the given key.
 *
 * @param image The image to store
 * @param key   The unique image cache key, usually it's image absolute URL
 */
- (void)storeImage:(UIImage *)image forKey:(NSString *)key;

/**
 * Store an image into memory and optionally disk cache at the given key.
 *
 * @param image  The image to store
 * @param key    The unique image cache key, usually it's image absolute URL
 * @param toDisk Store the image to disk cache if YES
 */
- (void)storeImage:(UIImage *)image forKey:(NSString *)key toDisk:(BOOL)toDisk;

/**
 * Store an image into memory and optionally disk cache at the given key.
 *
 * @param image       The image to store
 * @param recalculate BOOL indicates if imageData can be used or a new data should be constructed from the UIImage
 * @param imageData   The image data as returned by the server, this representation will be used for disk storage
 *                    instead of converting the given image object into a storable/compressed image format in order
 *                    to save quality and CPU
 * @param key         The unique image cache key, usually it's image absolute URL
 * @param toDisk      Store the image to disk cache if YES
 */
- (void)storeImage:(UIImage *)image recalculateFromImage:(BOOL)recalculate imageData:(NSData *)imageData forKey:(NSString *)key toDisk:(BOOL)toDisk;

/**
 * Query the disk cache asynchronously.
 *
 * @param key The unique key used to store the wanted image
 */
- (NSOperation *)queryDiskCacheForKey:(NSString *)key done:(SDWebImageQueryCompletedBlock)doneBlock;

/**
 * Query the memory cache synchronously.
 *
 * @param key The unique key used to store the wanted image
 */
- (UIImage *)imageFromMemoryCacheForKey:(NSString *)key;

/**
 * Query the disk cache synchronously after checking the memory cache.
 *
 * @param key The unique key used to store the wanted image
 */
- (UIImage *)imageFromDiskCacheForKey:(NSString *)key;

/**
 * Remove the image from memory and disk cache synchronously
 *
 * @param key The unique image cache key
 */
- (void)removeImageForKey:(NSString *)key;

/**
 * Remove the image from memory and disk cache synchronously
 *
 * @param key             The unique image cache key
 * @param completionBlock An block that should be executed after the image has been removed (optional)
 */
- (void)removeImageForKey:(NSString *)key withCompletition:(void (^)())completion;

/**
 * Remove the image from memory and optionally disk cache synchronously
 *
 * @param key      The unique image cache key
 * @param fromDisk Also remove cache entry from disk if YES
 */
- (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk;

/**
 * Remove the image from memory and optionally disk cache synchronously
 *
 * @param key             The unique image cache key
 * @param fromDisk        Also remove cache entry from disk if YES
 * @param completionBlock An block that should be executed after the image has been removed (optional)
 */
- (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk withCompletition:(void (^)())completion;

/**
 * Clear all memory cached images
 */
- (void)clearMemory;

/**
 * Clear all disk cached images. Non-blocking method - returns immediately.
 * @param completionBlock An block that should be executed after cache expiration completes (optional)
 */
- (void)clearDiskOnCompletion:(void (^)())completion;

/**
 * Clear all disk cached images
 * @see clearDiskOnCompletion:
 */
- (void)clearDisk;

/**
 * Remove all expired cached image from disk. Non-blocking method - returns immediately.
 * @param completionBlock An block that should be executed after cache expiration completes (optional)
 */
- (void)cleanDiskWithCompletionBlock:(void (^)())completionBlock;

/**
 * Remove all expired cached image from disk
 * @see cleanDiskWithCompletionBlock:
 */
- (void)cleanDisk;

/**
 * Get the size used by the disk cache
 */
- (NSUInteger)getSize;

/**
 * Get the number of images in the disk cache
 */
- (NSUInteger)getDiskCount;

/**
 * Asynchronously calculate the disk cache's size.
 */
- (void)calculateSizeWithCompletionBlock:(void (^)(NSUInteger fileCount, NSUInteger totalSize))completionBlock;

/**
 * Check if image exists in cache already
 */
- (BOOL)diskImageExistsWithKey:(NSString *)key;

/**
 *  Get the cache path for a certain key (needs the cache path root folder)
 *
 *  @param key  the key (can be obtained from url using cacheKeyForURL)
 *  @param path the cach path root folder
 *
 *  @return the cache path
 */
- (NSString *)cachePathForKey:(NSString *)key inPath:(NSString *)path;

/**
 *  Get the default cache path for a certain key
 *
 *  @param key the key (can be obtained from url using cacheKeyForURL)
 *
 *  @return the default cache path
 */
- (NSString *)defaultCachePathForKey:(NSString *)key;

  不要看着想吐就行。先看看使用吧。

 存图片:

 SDImageCache *imageCache = [SDImageCache sharedImageCache];

   [imageCache storeImage:image forKey:@"myphoto" toDisk:YES];

取图片:

 SDImageCache *imageCache = [SDImageCache sharedImageCache];
    UIImage *image = [imageCache imageFromDiskCacheForKey:@"myphoto"];

这样就可以取到自己存的图片了。可以看一下SDImageCache.h这个类了,里面提供了存图片到内存和磁盘的方法,还有取图片的方法,以及判断该图片是否存在的方法。还有就是删除图片、清空磁盘缓存、得到缓存大小等方法。

场景三、做设置中的清除缓存功能

  简单来说,当我们点击清除缓存按钮的时候会触发的消息如下:

- (void)clearCaches
{
    [MBProgressHUD showMessage:@"正在清理.."];

    [[SDImageCache sharedImageCache] clearMemory];
    [[SDImageCache sharedImageCache] clearDisk];

    [self performSelectorOnMainThread:@selector(cleanCacheSuccess) withObject:nil waitUntilDone:YES];
}

- (void)cleanCacheSuccess


{


    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{



        self.cacheLab.text = @"0.00M";


        return;

   });


}

很简单,就是把使用SDWebImage缓存的图片都清空就可以了,这里包括了内存中的缓存图片以及磁盘上的缓存图片。

 场景四、一个APP多用户缓存存储

  比如一个用户下面有好多只属于个人的图片,那么如果我们想做到当切换用户再切换当前用户还可以看到原来用户的图片时,就可以用到SDImageCache的命名空间功能。其实很简单

 1、简单说一下:当我们使用SDImageCache去缓存图片的时候,如果我们是这样初始化的SDImageCache:

    SDImageCache *image = [SDImageCache sharedImageCache];

那么我们会在Caches(沙盒)文件夹下看到一个com.hackemist.SDWebImageCache.default文件夹,这里就是用来存储sharedImageCache初始化的缓存存储的图片。如果我们是这样初始化SDImageCache:

  SDImageCache *imageCache = [[SDImageCache alloc] initWithNamespace:@"TTTTTT"];

那么我们会在Caches文件夹下看到一个com.hackemist.SDWebImageCache.TTTTT文件夹。这里就是用来存储这样初始化的缓存存储的图片。

其他看一下源码可以知道,它是根据命名空间来命名文件夹的。

这样我们就可以用用户名作为命名空间名称,这样每次取缓存都用用户名就OK了。

还有其他的使用场景,这里就不再赘述了。

四、实现原理 

 其实SDWebImage之所以能够实现缓存的原理关键就是在哪个key值。

  比如我们在使用

- (void)sd_setImageWithPreviousCachedImageWithURL:(NSURL *)url andPlaceholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock;

的时候,其实就是把url当做了一个图片的key值,然后存储对应的图片,如果下次请求的url和这次请求的url一样,那么就直接根据url(这个key)来取图片,如果url作为key的图片缓存不存在,就去请求远程服务器,然后请求过来之后再次将url和图片对应,然后存储。

 

五、总结

 

六、其他

  未完待续。

时间: 2024-05-11 00:08:06

SDWebImage的简单使用的相关文章

SDWebImage ReadMe.md文档简单说明

SDWebImage ReadMe.md 文档 附:SDWebImage框架github下载地址:https://github.com/rs/SDWebImage 注1:该文章简单翻译了SDWebImage最新版本(3.7.5)的readMe.md,时间紧促,如有不当请指正修改,十分感激. 注2:对该框架的学习将持续进行,在个人的github地址可以得到最新进展. Web Image 版本3.7.5|平台iOS|开源协议MIT| 该库为UIImageView提供了一个分类来处理远程图片资源的加载

模仿SDWebImage实现异步加载图片

模仿SDWebImage实现异步加载图片 SDWebImage想必大家都不陌生吧,要实现它的图片异步加载功能这个还是很简单的. 注意:此处我只实现了异步加载图片,并没有将文件缓存到本地的打算哦:) 源码: UIImageView+YXImageView.h // // UIImageView+YXImageView.h // PicDemo // // Copyright (c) 2014年 Y.X. All rights reserved. // #import <UIKit/UIKit.h>

IOS第三方之SDWebImage

项目中从服务器端下载图片这些几乎是必备的,使用时也很简单,只需引入SDWebImage文件 // // ViewController.m // sdWebImageDemo // // Created by City--Online on 15/6/15. // Copyright (c) 2015年 City--Online. All rights reserved. // #import "ViewController.h" #import "UIImageView+We

使用最新版SDWebImage

使用最新版SDWebImage 1. 下载源码: 2. 测试能否编译成功: 3. 用Xcode6新建一个工程,然后将文件夹拖入到工程当中: 4. 查看其主要的源码,发现之前使用版本的方法都被弃用了: 5. 常规的使用:   以下就来解析源码: -源码实现的细节- -option参数的详解- 你想自己使用它的源码实现下载图片么?建议你还是不要这么做,当然,你可以尝试^_^ 忽略本地缓存的下载模式- 下载器源码的简单分析- 这些已经很够用了^_^,有什么需要了解的可以继续补充.

最新版SDWebImage的使用

我之前写过一篇博客,介绍缓存处理的三种方式,其中最难,最麻烦,最占内存资源的还是图片缓存,最近做的项目有大量的图片处理,还是采用了SDWebImage来处理,但是发现之前封装好的代码报错了.研究发现,是我用了新版的SDWebImage,好多方法都变了. 现在把代码贴出来,供大家参考.尤其是新手,看完这篇博客,图片缓存so easy.最后有demo供大家下载,先学习. 第一步,下载SDWebImage,导入工程.github托管地址https://github.com/rs/SDWebImage

Win 8照片应用欣赏与简单编辑图片

在Win8照片应用中点击想要访问的分类,进入照片预览界面,为方便触控屏用户,Win8采用了横向浏览的方式,我们可以看到多个文件夹,触控屏用户只要滑动手指就能顺畅预览精美图片,鼠标操作可以滑动滚轮,操作都非常方便. 如果需要放大和缩小浏览图片,触控屏用户可以用手指开合来轻松操控,键鼠用户可以点击界面右下角的"+/-"按钮,或者用"Ctrl+鼠标滚轮"灵活缩放图片视图. Win8照片应用缺省按文件夹浏览,在文件夹浏览界面中,点击鼠标右键,屏幕下方会弹出几个选项:按日期浏

ps设计中国水墨风简单的烟雾骏马海报效果

  ps设计中国水墨风简单的烟雾骏马海报效果!简单粗暴,几步即可完成,不管是景观.动物.还是人物都适用,非常棒的设计参考 分类: PS入门教程

简单介绍Python2.x版本中的cmp()方法的使用

  这篇文章主要介绍了简单介绍Python2.x版本中的cmp()方法的使用,然而该方法在Python3.x版本中已并不再内置...需要的朋友可以参考下 cmp()方法比较两个列表的元素. 语法 以下是cmp()方法的语法: ? 1 cmp(list1, list2) 参数 list1 -- 这是要进行比较的第一个列表 list2 -- 这是要进行比较的第二个列表 返回值 如果元素是相同类型的,执行比较,并返回结果.如果元素是不同的类型,检查,看看他们是否是数字 如果是数字必要时强制进行数字比较

ajax同步异步的简单实现

本文为大家介绍下ajax同步异步的简单实现,感兴趣的朋友可以参考下 复制代码 代码如下: $("#btn_saveFWSB").click(function(){  var obj=checkData(arr);  if(obj.flag==true){  hideAddDiv();  $.ajax({  type : "POST",  url : "/vts/doInsertFWZT.do",  async: false ,//ajax同步