Web Image
=========
This library provides a category for UIImageVIew with support for remote images coming from the web.
It provides:
- 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
- A guarantee that the same URL won't be downloaded several times
- A guarantee that bogus URLs won't be retried again and again
- Performances!
Motivation
----------
As a dummy Objective-C developer working on my first iPhone application for my company
([Dailymotion][]), I've been very frustrated by the lack of support in the Cocoa Touch framework for
UITableView with remote images. After some Googling, I found lot of forums and blogs coming up with
their solution, most of the time based on asynchronous usage with NSURLConnection, but none provided
a simple library doing the work of async image grabbing + caching for you.
Actually there is one in the famous [Three20][] framework by [Joe Hewitt][], but it's a massive
and undocumented piece of code. You can't import just the the libraries you want without taking the
whole framework (damn #import "TTGlobal.h"). Anyway, the [Three20][] implementation is based on
NSURLConnection, and I soon discovered this solution wasn't ideal. Keep reading to find out why.
As a hurried beginner in iPhone development, I couldn't attempt to implement my own async image
grabber with caching support as my first steps in this new world. Thus, I asked for help from my good
friend Sebastien Flory ([Fraggle][]), who was working on his great iPhone game ([Urban Rivals][], a
future app-store hit) for almost a year. He spent quite an amount of time implementing the very
same solution for his needs, and was kind enough to give me his implementation for my own use. This
worked quite well and allowed me to concentrate on other parts of my application. But when I started
to compare my application with its direct competitor - the built-in Youtube application - I was very
unhappy with the loading speed of the images. After some network sniffing, I found that every HTTP
requests for my images was 10 times slower than Youtube's... On my own network, Youtube was 10
time faster than my own servers... WTF??
In fact, my servers were fine but a lot of latency was added to the requests, certainly because my
application wasn't responsive enough to handle the requests at full speed. Right then, I
understood something important, asynchronous NSURLConnections are tied to the main runloop in the
NSEventTrackingRunLoopMode. As explained in the documentation, this runloop mode is affected by
UI events:
> Cocoa uses this mode to restrict incoming events during mouse-dragging loops and other sorts of
> user interface tracking loops.
A simple test to recognize an application using NSURLConnection in its default mode to load
remote images is to scroll the UITableView with your finger to disclose an unloaded image, and to
keep your finger pressed on the screen. If the image doesn't load until you release you finger,
you've got one (try with the Facebook app for instance). It took me quite some time to understand
the reason for this lagging issue. Actually I first used NSOperation to workaround this issue.
This technique combined with an image cache instantly gave a lot of responsiveness to my app.
I thought this library could benefit other Cocoa Touch applications so I open-sourced it.
How To Use It
-------------
### Using UIImageView+WebCache category with UITableView
Just #import the UIImageView+WebCache.h header, and call the setImageWithURL:placeholderImage:
method from the tableView:cellForRowAtIndexPath: UITableViewDataSource method. Everything will be
handled for you, from async downloads to caching management.
#import "UIImageView+WebCache.h"
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *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.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
cell.textLabel.text = @"My Text";
return cell;
}
### Using blocks
If your project's deployement target is set to iOS 4+, you may want to use the success/failure blocks to be
notified when image have been retrieved from cache.
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
success:^(UIImage *image) {... success code here ...}
failure:^(NSError *error) {... failure code here ...}];
];
Note: neither your success nor failure block will be call if your image request is canceled before completion.
### Using SDWebImageManager
The SDWebImageManager is the class behind the UIImageView+WebCache category. It ties the
asynchronous downloader with the image cache store. You can use this class directly to benefit
from web image downloading with caching in another context than a UIView (ie: with Cocoa).
Here is a simple example of how to use SDWebImageManager:
SDWebImageManager *manager = [SDWebImageManager sharedManager];
UIImage *cachedImage = [manager imageWithURL:url];
if (cachedImage)
{
// Use the cached image immediatly
}
else
{
// Start an async download
[manager downloadWithURL:url delegate:self];
}
Your class will have to implement the SDWebImageManagerDelegate protocol, and to implement the
webImageManager:didFinishWithImage: method from this protocol:
- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image
{
// Do something with the downloaded image
}
### Using Asynchronous Image Downloader Independently
It is possible to use the async image downloader independently. You just have to create an instance
of SDWebImageDownloader using its convenience constructor downloaderWithURL:delegate:.
downloader = [SDWebImageDownloader downloaderWithURL:url delegate:self];
The download will start immediately and the imageDownloader:didFinishWithImage: method from the
SDWebImageDownloaderDelegate protocol will be called as soon as the download of image is completed.
### Using Asynchronous Image Caching Independently
It is also possible to use the NSOperation based image cache store independently. SDImageCache
maintains a memory cache and an optional disk cache. Disk cache write operations are performed
asynchronous so it doesn't add unnecessary latency to the UI.
The SDImageCache class provides a singleton instance for convenience but you can create your own
instance if you want to create separated cache namespace.
To lookup the cache, you use the imageForKey: method. If the method returns nil, it means the cache
doesn't currently own the image. You are thus responsible for generating and caching it. The cache
key is an application unique identifier for the image to cache. It is generally the absolute URL of
the image.
UIImage *myCachedImage = [[SDImageCache sharedImageCache] imageFromKey:myCacheKey];
By default SDImageCache will lookup the disk cache if an image can't be found in the memory cache.
You can prevent this from happening by calling the alternative method imageFromKey:fromDisk:
没有合适的资源?快使用搜索试试~ 我知道了~
IOS使用UIImageView显示gif动画的例子修改
共60个文件
h:17个
m:14个
plist:4个
5星 · 超过95%的资源 需积分: 15 120 下载量 117 浏览量
2012-03-23
11:06:20
上传
评论 3
收藏 351KB ZIP 举报
温馨提示
其中用到了两个开源代码 1.SDWebImage 异步读取图片数据 2.SCGifExample gif动画控件 此工程在SCGifExample的基础上修改 并在上一版基础上修改了部分功能
资源推荐
资源详情
资源评论
收起资源包目录
SCGifExample.zip (60个子文件)
SCGifExample
SCGifExampleViewController.h 1KB
SCGifExample-Info.plist 1KB
.DS_Store 6KB
wiRoundedRectImage.h 372B
wiAutoSizeView.h 1KB
SCGifExampleViewController.m 11KB
en.lproj
MainWindow.xib 20KB
SCGifExampleViewController.xib 23KB
InfoPlist.strings 45B
main.m 351B
header_imglook.png 1KB
SCGIFImageView.h 3KB
1.jpg 134KB
SCGifExampleAppDelegate.m 3KB
wiCommonDef.m 4KB
wiAutoSizeView.m 6KB
SCGIFImageView.m 17KB
1.gif 112KB
SCGifExample.xcodeproj
project.xcworkspace
contents.xcworkspacedata 157B
xcuserdata
qq.xcuserdatad
WorkspaceSettings.xcsettings 383B
UserInterfaceState.xcuserstate 48KB
jackie.xcuserdatad
WorkspaceSettings.xcsettings 383B
UserInterfaceState.xcuserstate 154KB
xcuserdata
qq.xcuserdatad
xcdebugger
Breakpoints.xcbkptlist 91B
xcschemes
xcschememanagement.plist 484B
SCGifExample.xcscheme 3KB
jackie.xcuserdatad
xcschemes
xcschememanagement.plist 484B
SCGifExample.xcscheme 3KB
project.pbxproj 24KB
btn_imglook_heaher(active).png 1KB
btn_imglook_heaher.png 1KB
SCGifExample-Prefix.pch 358B
wiCommonDef.h 1KB
SCGifExampleAppDelegate.h 426B
wiRoundedRectImage.m 3KB
rs-SDWebImage-580a90d
.DS_Store 6KB
SDImageCache.h 1KB
UIButton+WebCache.m 2KB
SDWebImageManager.h 2KB
UIImageView+WebCache.h 4KB
SDImageCache.m 11KB
SDWebImageManagerDelegate.h 2KB
SDWebImageDecoder.m 4KB
SDWebImagePrefetcher.h 1KB
UIImageView+WebCache.m 4KB
SDWebImageDownloader.m 6KB
LICENSE 1KB
SDWebImageDownloader.h 1KB
SDWebImageManager.m 17KB
SDWebImageCompat.h 3KB
README.md 10KB
SDWebImage.xcodeproj
project.xcworkspace
contents.xcworkspacedata 155B
xcuserdata
qq.xcuserdatad
xcschemes
xcschememanagement.plist 482B
SDWebImage.xcscheme 2KB
project.pbxproj 17KB
UIButton+WebCache.h 4KB
SDWebImagePrefetcher.m 3KB
SDWebImageDecoder.h 929B
SDWebImageDownloaderDelegate.h 617B
SDImageCacheDelegate.h 506B
共 60 条
- 1
iunion
- 粉丝: 283
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于Matlab人脸肤色定理的教师人数统计+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab霍夫曼变换的表盘读数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab火灾烟雾检测源码带GUI界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的恶劣天气交通标志识别系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的霍夫曼变换的表盘示数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的车道线识别系统 +源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的教室人数统计系统带Gui界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的教室人数统计系统带Gui界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB 的霍夫曼变换答题卡识别源码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab+bp神经网络的神经网络汉字识别系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页