IOS 加载网络图片2
//1. NSData dataWithContentsOfURL
// [self.icon setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:tempUrl]]]; //2. dispath形式添加到异步处理
// [self imageDownLoadByUrlASYNC:tempUrl Complete:^(UIImage *image) {
// [self.icon setImage:image];
// }];
//3. 当前我所选用的方式 边下载边加载的方式 用的CGImageRef imageWithCGImage
_request = [[NSURLRequest alloc] initWithURL:tempUrl];
_conn = [[NSURLConnection alloc] initWithRequest:_request delegate:self]; _incrementallyImgSource = CGImageSourceCreateIncremental(NULL); _recieveData = [[NSMutableData alloc] init];
_isLoadFinished = false; self.icon.alpha = .5;
self.lblTitle.alpha = .5;
[self.lblTitle setText:appName];
第一种方式,是基本上很少有人用的 是最基础的方式 这种方式有个问题 就是网络不好的情况下会卡主线程,导致程序假死
第二种方式,请款这段实现代码
//
//-(void)imageDownLoadByUrlASYNC:(NSURL *)url Complete:(complete)finished
//{
// //异步并列执行
// dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// UIImage *image = nil;
// NSError *error;
// NSData *responseData = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:&error];
// image = [UIImage imageWithData:responseData];
// //跳回主队列执行
// dispatch_async(dispatch_get_main_queue(), ^{
// //在主队列中进行ui操作
// finished(image);
// });
//
// });
//}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error;
UIImage *image = nil;
SDWebImageManager *manager = [SDWebImageManager sharedManager];
BOOL existBool = [manager diskImageExistsForURL:url];//判断是否有缓存
if (existBool) {
image = [[manager imageCache] imageFromDiskCacheForKey:url.absoluteString];
}else{
NSData *responseData = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:&error];
image = [UIImage imageWithData:responseData];
}
//跳回主队列执行
dispatch_async(dispatch_get_main_queue(), ^{
//在主队列中进行ui操作
__block CGFloat itemW = mWidth;
__block CGFloat itemH = 0;
NSLog(@"%f",image.size.width);
//根据image的比例来设置高度
if (image.size.width) {
itemH = image.size.height / image.size.width * itemW;
if (itemH >= itemW) {
if (itemH >= mHeight-64) {
NSLog(@"%f",[UIScreen mainScreen].bounds.size.width);
UIScrollView *sv = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, itemW, mHeight-64)];
sv.contentSize = CGSizeMake(itemW, itemH);
sv.showsVerticalScrollIndicator = NO;
sv.userInteractionEnabled = YES;
[self.view addSubview:sv];
imageView.frame = CGRectMake(0, 0, itemW, itemH);
imageView.contentMode = UIViewContentModeScaleAspectFit;
[sv addSubview:imageView];
}else{
imageView.frame = CGRectMake(0, 64, itemW, itemH);
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
}
}else{
imageView.frame = CGRectMake(0, 64, itemW, itemH);
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
}
}else{
imageView.frame = CGRectMake(0, 64, mWidth, mHeight-64);
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
}
[self.myView stopAnimating];
});
});
虽然情况跟第一种实现一样,但是将执行代码添加到对应的异步执行中 然后再成功下载之后 获取到image之后 放到主线程执行回调 设置image
第三种方式 需要以下代码 这是我百度到的方式
#pragma mark -- NSURLConnectionDataDelegate -(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
_expectedLeght=response.expectedContentLength;
NSLog(@"expectedLength:%lld",_expectedLeght); NSString*mimeType=response.MIMEType;
NSLog(@"MIMETYPE%@",mimeType); NSArray*arr=[mimeType componentsSeparatedByString:@"/"];
if(arr.count<1||![[arr objectAtIndex:0] isEqual:@"image"])
{
NSLog(@"notaimageurl");
[connection cancel];
_conn=nil;
}
}
-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
NSLog(@"Connection%@error,errorinfo:%@",connection,error);
}
-(void)connectionDidFinishLoading:(NSURLConnection*)connection
{
NSLog(@"ConnectionLoadingFinished!!!");
//ifdownloadimagedatanotcomplete,createfinalimage
if(!_isLoadFinished){
CGImageSourceUpdateData(_incrementallyImgSource,(CFDataRef)_recieveData,_isLoadFinished);
CGImageRef imageRef=CGImageSourceCreateImageAtIndex(_incrementallyImgSource,0,NULL);
UIImage * image=[UIImage imageWithCGImage:imageRef];
[self.icon setImage:image];
CGImageRelease(imageRef);
}
}
-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
[_recieveData appendData:data]; _isLoadFinished=false;if(_expectedLeght==_recieveData.length){
_isLoadFinished=true;
} CGImageSourceUpdateData(_incrementallyImgSource,(CFDataRef)_recieveData,_isLoadFinished);
CGImageRef imageRef=CGImageSourceCreateImageAtIndex(_incrementallyImgSource,0,NULL);
UIImage * image=[UIImage imageWithCGImage:imageRef];
[self.icon setImage:image];
CGImageRelease(imageRef);
}
这个方法经过我测试了 非常好用 但是不知道会不会有什么bug 只是刚使用 并且用户体验也会相应增加
IOS 加载网络图片2的更多相关文章
- ios UIImageView异步加载网络图片
方法1:在UI线程中同步加载网络图片 UIImageView *headview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 4 ...
- UIImageView异步加载网络图片
在iOS开发过程中,经常会遇到使用UIImageView展现来自网络的图片的情况,最简单的做法如下: 去下载https://github.com/rs/SDWebImage放进你的工程里,加入头文件# ...
- 【WPF】wpf image控件加载网络图片不显示问题,
1.加载网络图片到内存system.drawing.image对象中2.内存中的image 转Bitmap 再转适合system.windows.controls.image 的BitmapImage ...
- 有关DTCoreText无法加载网络图片及应用问题
至于DTCoreText是干嘛的,不清楚的同学自行网上脑补,这就不啰嗦了,只说一下其用法. 里面有三种控件供大家使用,DTAttributedTextView, DTAttributedLabel 和 ...
- [原创]cocos2dx加载网络图片&异步加载图片
[动机] 之前看到一款卡牌游戏,当你要看全屏高清卡牌的时候,游戏会单独从网络上下载,本地只存了非高清的,这样可以省点包大小,所以我萌生了实现一个读取网络图片的类. [联想] 之前浏览网页的时候经常看到 ...
- SDWebImage 加载网络图片失败,重新运行,就能加载成功。
现象: 使用SDWebImage 加载网络图片,偶尔会有一两张图片就是显示不出来.重新运行有时又可以了. 这个问题的原因是: 当SDWebImage 在加载图片的时候 我用的是- (void)sd_s ...
- Android Volley入门到精通:使用Volley加载网络图片
在上一篇文章中,我们了解了Volley到底是什么,以及它的基本用法.本篇文章中我们即将学习关于Volley更加高级的用法,如何你还没有看过我的上一篇文章的话,建议先去阅读Android Volley完 ...
- Android三种基本的加载网络图片方式(转)
Android三种基本的加载网络图片方式,包括普通加载网络方式.用ImageLoader加载图片.用Volley加载图片. 1. [代码]普通加载网络方式 ? 1 2 3 4 5 6 7 8 9 10 ...
- 仿微信朋友圈图片查看-glide加载网络图片,photoview 实现缩放
http://www.cnblogs.com/csonezp/p/5083286.html 这里实现的效果就和微信朋友圈点击图片后查看大图一样,如果你不清楚是什么效果,可以拿出手机,打开朋友圈,找到一 ...
随机推荐
- CentOS6.8安装mysql5.6
一.下载mysql5.6 下载地址,并上传至Linux 二.查看CentOS是否自带的mysql rpm -qa | grep mysql 三.卸载自带的mysql rpm -e --nodeps m ...
- MongoDB学习之--安全和认证
MongoDB学习之--安全和认证 本文主要介绍两部分内容,Mongodb的安全检查配置以及安全认证操作: 虽然确保系统安全是系统管理员的重要工作,但是作为程序员了解其机制也是大有好处的,毕竟不是每个 ...
- 多个AsynceTask无法同时运行的现象分析
关于这篇博客所提到的问题是在一段再简单不过的代码中意外出现的.当时我使用了两个不同'AsyncTask'帮助我执行两个需要在后台执行任务.并且这两个'AsyncTask'几乎是同时运行的.原本会正常运 ...
- 基本Guava工具
使用Joiner类 将任意字符串通过分隔符进行连接到一起是大多程序员经常做的事情.他们经常使用array,list,iterable并且循环变量将每一个临时变量添加到StringBuilder当中 ...
- ORM查询语言(OQL)简介高级篇
ORM查询语言(OQL)简介--高级篇:脱胎换骨 在写本文之前,一直在想文章的标题应怎么取.在写了<ORM查询语言(OQL)简介--概念篇>.<ORM查询语言(OQL)简介--实例篇 ...
- Http的四种post方式
1.引言 HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.TRACE.CONNECT 这几种.其中 POST 一般用来向服务端提交 ...
- WPF界面设计
WPF仿360卫士9.0界面设计 Chrome插件——一键保存网页为PDF1.0 http://www.cnblogs.com/bdstjk/p/3163723.html 仿照网上的一个代码写的, ...
- [每日一题] OCP1z0-047 :2013-07-15 drop column
如下实验: gyj@OCM> Create table emp( 2 Empno number(4) not null, 3 First_name varchar2( ...
- hdu3415 Max Sum of Max-K-sub-sequence
Max Sum of Max-K-sub-sequence Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64 ...
- Embedded Linux Primer----嵌入式Linux基础教程--导论
第一章 导论 在这一章里(将要学习到) 为什么是Linux 嵌入式Linux现状 开源和GPL(译者:通用公共许可证) 标准和有关团体 本章总结 放弃专有操作系统正在许多传统嵌入式操作系统公司引起一阵 ...