您的位置:首页 > 移动开发 > IOS开发

SDWebImage使用入门

2015-07-17 06:47 483 查看
 

为什么要用它

(原文地址:http://blog.csdn.net/niu_gao)
SDWebImage是一个从网站下载图片的库,它扩展了UIImageView类。UIImageView本来就可以直接从网站下载图片,但是它没有考虑对图片进行缓存,导致只要显示图片就要访问网络。而SDWebImage就解决了这个问题(当然不止这一个功能)。

下载位置

https://github.com/rs/SDWebImage

编译

下载后直接打开工程进行编译即可。

被其它工程使用

要在其它工程中使用有多种方式,最简单的就是把类,工程的引用添加到其它工程中。
 
步骤如下:
l  在其它工程中,工程名字上点出弹出菜单。
l  选择Add files to “XXX”。
l  选择SDWebImage的工程文件,选中Copy items if needed。
l  点Add。
 
编译,通不过。因为缺少了一个库的源码,这个库叫libwebp。位置:https://github.com/webmproject/libwebp
请下载0.4.3版,否则编译通不过。我说的是现在,过上几个月可以就需更高版本了。
 
把libwebp的src目录复制到SDWebImage中的Vendors/libwebp下。再编译,通过。
 
但是,运你的工程会崩溃(前提是你的工程中使用了SDWebImage的功能)。还需要添加链接选项:-ObjC。
 
如果此时运行依然出错,则还需要在“BuildPhases”中添加对ImageIO.framework的引用。
 
代码示例:#import<SDWebImage/UIImageView+WebCache.h> ...  - (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];     }      // 我们使用sd_setImageWithURL: 来加载web图像    [cell.imageViewsd_setImageWithURL:[NSURL                    URLWithString:@"http://www.domain.com/path/to/image.jpg"]                                           placeholderImage:[UIImage imageNamed:@"placeholder.png"]];     cell.textLabel.text= @"My Text";    return cell; }
 
(原文地址:http://blog.csdn.net/niu_gao)

使用block//我们使用sd_setImageWithURL: 来加载web图像
[cell.imageView sd_setImageWithURL:
        [NSURLURLWithString:@"http://www.domain.com/path/to/image.jpg"]
         placeholderImage:[UIImage imageNamed:@"placeholder.png"]       
        completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
               ... completion code here ...
        }
];
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  SDWebImage ios xcode