在开发中经常会遇到列表加载的功能,其中大部分都包括图片列表加载,但移动设备本身内存有限,而大量图片加载又很耗内存。今天主要就介绍一个第三方图片异步加载库SDWebImage,Github地址为:https://github.com/rs/SDWebImage,这个库主要实现了为UIImageView添加一个类别方法,让使用者使用图片异步加载就好像直接为UIImageView设置image一样,使用非常方便。
一、主要功能
- An UIImageView category adding web image and cache management to the Cocoa Touch framework
- An asynchronous image downloader
- An asynchronous memory + disk image caching with automatic cache expiration handling
- Animated GIF support
- WebP format support
- A background image decompression
- A guarantee that the same URL won't be downloaded several times
- A guarantee that bogus URLs won't be retried again and again
- A guarantee that main thread will never be blocked
- Performances!
- Use GCD and ARC
包括图片异步下载器、内存和本地缓存、GIF支持、相同URL不会重复下载、不会阻塞UI线程等。
二、集成使用步骤
- Download and unzip the last version of the framework from thedownload
page(下载framwork包并解压)
- Right-click on the project navigator and select "Add Files to "Your Project":(右击工程选择添加库到项目工程)
- In the dialog, select SDWebImage.framework:(选择SDWebImage.framwork库)
- Check the "Copy items into destination group's folder (if needed)" checkbox(选择拷贝到工程复选框)
- 添加ImageIO.framework库支持
- 添加Linker Flag,如下图
按下Command+B,编译工程,如果编译成功则说明集成成功了,如果出现framwork找不到的异常,则将工程目录中SDWebImage-3.3.framwork改名为SDWebImage.framwork再编译就没问题了,由于我下的是最新3.3版本,可能原配置文件搜索的编译路径名是不带“-3.3”的,所以提一下这个问题。配置和编译成功后就可以开始使用了:
三、Demo实现
先看看最终实现的效果图,主要实现了UITableView的列表加载和单个图片的异步加载:
在需要使用异步图片加载的头文件导入:
#import <SDWebImage/UIImageView+WebCache.h>
列表实现在UITableView的协议方法中只需要很简单的调用UIImageView的类别方法即可完成异步加载:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *Identifier = @"UITableViewIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
}
NSInteger row = indexPath.row;
cell.textLabel.text = @"My Text";
[cell.imageView setImageWithURL:[arr_imgs objectAtIndex:row]];
return cell;
}
单击UITableView的item进入单个图片加载页面,使用SDWebImageDownloader加载:
//单独异步下载一个图片
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:self.imgURL] options:0 progress:^(NSUInteger receivedSize, long long expectedSize) {
} completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
if (image && finished) {
imageView.image = image;
}
}];
以上只给出关键代码,具体Demo详情请下载工程源码:
下载
加入我们的或微信公众账号请查看:Ryan's
zone公众账号及
同时欢迎关注我的新浪微博和我交流:@唐韧_Ryan
觉得这篇文章对你有用就顶我一下吧!
分享到:
相关推荐
这是一个第三方图片异步加载库SDWebImage,Github地址为:https://github.com/rs/SDWebImage,这个库主要实现了为UIImageView添加一个类别方法,让使用者使用图片异步加载就好像直接为UIImageView设置image一样,...
在开发中经常会遇到列表加载的功能,其中大部分都包括图片列表加载,但移动设备本身内存有限,而大量图片加载又很耗内存。今天主要就介绍一个第三方图片异步加载库SDWebImage
一个很好用的异步加载图片的ios类库,引用超级方便,我很喜欢
自测过,可以用。SDWebImage 异步加载图片 多线程刷新界面
IOS应用源码——下拉动态加载.zip
IOS应用源码——下拉动态加载.rar
这是一个异步加载 图片缓存管理的小例子 简化对网络图片加载的处理
IOS应用源码——圆形Activity加载器.zip
IOS应用源码——圆形Activity加载器.rar
之前XML解析的托展,这次用的是TBXML的第三方解析的。(可能服务器问题,获取的数据有错,但方法是没问题的) 然后用IconDownloader异步加载得到的图片流。
IOS应用源码——通过OpenGL加载图像 DrawPixels.zip
一个iOS异步加载网络图片的Demo,用Objective-C写的,包含了三种加载网络图片的方式:同步、异步和Cache异步。界面布局全部代码手写,使用的是TableView布局。代码有注释,很简洁。欢迎大家下载使用。
IOS应用源码——通过OpenGL加载图像 DrawPixels.rar
IOS应用源码——通过OpenGL加载图像 DrawPixels2.zip
本篇文章主要介绍了iOS 图片加载框架SDWebImage解读,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
IOS应用源码——通过OpenGL加载图像 DrawPixels2.rar
iOS 图片异步加载 图片缓存