iOS UIImage DownLoad图片的下载缓存全部在此
iOS图片的下载缓存全部在此
分类: iOS编程 -- : 2075人阅读 评论() 收藏 举报
注意: 我的文章只写给自己看
----------------------------------------------------------------------------------------
(一)这部分(感觉out了), 但是还是保留, 算是学习的痕迹.
----------------------------------------------------------------------------------------
()最简单的下载,显示图片的方法:
[plain] view plaincopy
UIImageView *imageView = [[UIImageView alloc] initWithFrame:[self.view bounds]];
imageView.image = [self loadImageFromUrl:@"http://storage.live.com/items/72A00BF5A838647C!1616?filename=meinv004.jpg"];
[self.view addSubview:imageView]; -(UIImage*)loadImageFromUrl: (NSString*)url
{
NSURL *imageUrl = [NSURL URLWithString:url];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
UIImage *image = [UIImage imageWithData:imageData];
return image;
} 这种最简单的图片加载方式阻塞了main线程. 使得流程不能流畅进行. ()开辟线程来解决这个问题. [plain] view plaincopy
// set imageview
UIImageView *imageView = [[UIImageView alloc] initWithFrame:[self.view bounds]];
imageView.backgroundColor = [UIColor yellowColor];
imageView.tag = imageView_tag;
[self.view addSubview:imageView]; // load image in background
NSString *url = IMAGE_URL;
[self performSelectorInBackground:@selector(loadImageFromUrl:) withObject:url]; -(void)loadImageFromUrl: (NSString*)url {
NSURL *imageUrl = [NSURL URLWithString:url];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
[self performSelectorOnMainThread:@selector(updateImageView:) withObject:imageData waitUntilDone:NO];
}
-(void) updateImageView:(NSData*) data {
UIImageView *imageView = (UIImageView *)[self.view viewWithTag:imageView_tag];
imageView.image = [UIImage imageWithData:data];
} 并且只能在main线程中设置UI的内容, 所以代码量增加了较多. 代码量暂且不管, 这里还有一个比较严重的问题就是每次都要加载图片, 使用SDWebImage:
[plain] view plaincopy
#import <SDWebImage/UIImageView+WebCache.h>
[imageView setImageWithURL:[NSURL URLWithString:[_objects objectAtIndex:indexPath.row]]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
SDWebImage可以实现:
*下载和缓存图片.
*相同的url不会被重复下载.
*坏的url不会一直请求. 使用HJCache:
[plain] view plaincopy
// 目前HJCache不支持ARC, 所以这是个问题. -----------------------------------------------------------------------------------------------------------------
(二)多线程初步实现TableView的图片显示(之前用第三库老是不稳定) 这个算是比较满意的.
------------------------------------------------------------------------------------------------------------------------
[plain] view plaincopy
@interface c:NSOperation @property NSString *url;
@property NSString *imageName;
@property UIImage *image;
@property UIImageView *delegate; -(void) main;
-(id) initWith:(NSString *)url imageName:(NSString *)imageName delegate:(UIImageView *)delegate; @end @implementation c:NSOperation
@synthesize url = _url,imageName=_imageName, image=_image, delegate=_delegate; -(id) initWith:(NSString *)url imageName:(NSString *)imageName delegate:(UIImageView *)delegate{
if (self = [super init]) {
self.url = url;
self.imageName = imageName;
self.delegate = delegate;
}
return self;
} -(void) main{
//
NSString *cachefile = [NSTemporaryDirectory() stringByAppendingPathComponent: self.imageName];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.url]];
[data writeToFile:cachefile atomically:YES]; //
self.image = [UIImage imageWithData:data];
[self performSelectorOnMainThread:@selector(u) withObject:nil waitUntilDone:NO];
}
-(void)u{
[self.delegate setImage:self.image];
} [plain] view plaincopy
queue = [[NSOperationQueue alloc] init];//这是成员队列的实例化
设置TableView cell中的图片:
[plain] view plaincopy
NSString *filename = [NSString stringWithFormat:@"%d", indexPath.row];
NSString *cachefile = [NSTemporaryDirectory() stringByAppendingPathComponent: filename];
UIImage *image = [UIImage imageWithContentsOfFile:cachefile];
if (image) {
cell.imageView.image = image;
} else {
c *o = [[c alloc] initWith:[_objects objectAtIndex:indexPath.row] imageName:[NSString stringWithFormat:@"%d",indexPath.row] delegate:cell.imageView];
[queue addOperation:o];
cell.imageView.image= [UIImage imageNamed:@"placeholder.png"];
} 注: 保存一下测试图片 urls
http://blog.csdn.net/deep_explore/article/details/8144613
原网址
iOS UIImage DownLoad图片的下载缓存全部在此的更多相关文章
- ios UIImage 圆形图片剪切方案
@interface UIImage (Resize) //按形状切割图像 - (UIImage*)cutImageWithRadius:(int)radius; @end //图片剪切 - (UII ...
- iOS利用SDWebImage图片下载缓存
一.我们先来了解一下SDWebImage的使用: 1.导入框架,引入头文件: #import "UIImageView+WebCache.h" 也可以直接使用CocoaPods来引 ...
- ios -- cell的图片下载
1.面试题 1> 如何防止一个url对应的图片重复下载 * “cell下载图片思路 – 有沙盒缓存” 2> SDWebImage的默认缓存时长是多少? * 1个星期 3> SDWeb ...
- IOS开发-UI学习-根据URL显示图片,下载图片的练习(button,textfield,image view,url,data)
编写一个如下界面,实现: 1.在文本输入框中输入一个网址,然后点击显示图片,图片显示到UIImageView中. 2.点击下载,这张显示的图片被下载到手机的Documents文件夹下的Dowmload ...
- 毕加索的艺术——Picasso,一个强大的Android图片下载缓存库,OkHttpUtils的使用,二次封装PicassoUtils实现微信精选
毕加索的艺术--Picasso,一个强大的Android图片下载缓存库,OkHttpUtils的使用,二次封装PicassoUtils实现微信精选 官网: http://square.github.i ...
- ios中asihttprequest 下载缓存
asi中下载缓存第一种方法 #import <UIKit/UIKit.h> #import "ASIHTTPRequest.h" #import "ASIDo ...
- picasso_强大的Android图片下载缓存库
tag: android pic skill date: 2016/07/09 title: picasso-强大的Android图片下载缓存库 [本文转载自:泡在网上的日子 参考:http://bl ...
- android开源项目:图片下载缓存库picasso
picasso是Square公司开源的一个Android图形缓存库,地址http://square.github.io/picasso/,可以实现图片下载和缓存功能. picasso有如下特性: 在a ...
- iOS:实现图片的无限轮播(二)---之使用第三方库SDCycleScrollView
iOS:实现图片的无限轮播(二)---之使用第三方库SDCycleScrollView 时间:2016-01-19 19:13:43 阅读:630 评论:0 收藏:0 ...
随机推荐
- C++11用于计算函数对象返回类型的统一方法
[C++11用于计算函数对象返回类型的统一方法] 模板 std::result_of 被TR1 引进且被 C++11 所采纳,可允许我们决定和使用一个仿函数其回返值的类别.底下,CalculusVer ...
- 【转】删除已经存在的 TFS Workspace
删除已经存在的 TFS Workspace 分类: TFS2010-03-03 16:59 1239人阅读 评论(2) 收藏 举报 serverpathcommandcachefilegoogle 工 ...
- ASP.NET实例——漂亮的自适应宽度的导航条(仿Discuz!)
PHP比较成熟的开放的源代码比较多,比方说PrestaShop,比方说Discuz!...... 虽然语言不同,但基本原理是一样的,有时间的话读一读,对学习ASP.NET应该是非常有好处的(唉,什么时 ...
- android 绘图之Canvas,Paint类
Canvas,Paint 1.在android 绘图但中经常要用到Canvas和Paint类,Canvas好比是一张画布,上面已经有你想绘制图画的轮廓了,而Paint就好比是画笔,就要给Canvas进 ...
- 《Java程序员修炼之道》
原子类:java.util.concurrent.atomic 线程锁:java.util.concurrent.locks 对付死锁:boolean acquired = lock.tryLock( ...
- NBearV3中文教程总目录
1.NBearV3 Step by Step教程——ORM篇 摘要:本教程演示如何基于NBearV3的ORM模块开发一个Web应用程序的全过程.本教程演示的实体关系包括:继承.1对1关联.1对多关联, ...
- 【转】【Android测试技巧】01. root后adb shell默认不是root用户时,如何将文件放入手机系统中
http://blog.csdn.net/wirelessqa/article/details/8624208 有些机器root后通过adb shell 后,默认不是root用户,需要输入 su才能切 ...
- PL/pgSQL学习笔记之十
http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html 39.3.3. 类型拷贝 variable%TYPE %TYPE ...
- cmd下运行PowerShell命令
PowerShell -Command "& {Get-EventLog -LogName security}"
- Swift学习笔记十四
Deinitialization 当类的实例对象即将要被释放时,会立即调用deinitializer,通过deinit关键字来定义deinitializer,和initializer一样,它也只存在于 ...