//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的更多相关文章

  1. ios UIImageView异步加载网络图片

    方法1:在UI线程中同步加载网络图片 UIImageView *headview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 4 ...

  2. UIImageView异步加载网络图片

    在iOS开发过程中,经常会遇到使用UIImageView展现来自网络的图片的情况,最简单的做法如下: 去下载https://github.com/rs/SDWebImage放进你的工程里,加入头文件# ...

  3. 【WPF】wpf image控件加载网络图片不显示问题,

    1.加载网络图片到内存system.drawing.image对象中2.内存中的image 转Bitmap 再转适合system.windows.controls.image 的BitmapImage ...

  4. 有关DTCoreText无法加载网络图片及应用问题

    至于DTCoreText是干嘛的,不清楚的同学自行网上脑补,这就不啰嗦了,只说一下其用法. 里面有三种控件供大家使用,DTAttributedTextView, DTAttributedLabel 和 ...

  5. [原创]cocos2dx加载网络图片&异步加载图片

    [动机] 之前看到一款卡牌游戏,当你要看全屏高清卡牌的时候,游戏会单独从网络上下载,本地只存了非高清的,这样可以省点包大小,所以我萌生了实现一个读取网络图片的类. [联想] 之前浏览网页的时候经常看到 ...

  6. SDWebImage 加载网络图片失败,重新运行,就能加载成功。

    现象: 使用SDWebImage 加载网络图片,偶尔会有一两张图片就是显示不出来.重新运行有时又可以了. 这个问题的原因是: 当SDWebImage 在加载图片的时候 我用的是- (void)sd_s ...

  7. Android Volley入门到精通:使用Volley加载网络图片

    在上一篇文章中,我们了解了Volley到底是什么,以及它的基本用法.本篇文章中我们即将学习关于Volley更加高级的用法,如何你还没有看过我的上一篇文章的话,建议先去阅读Android Volley完 ...

  8. Android三种基本的加载网络图片方式(转)

    Android三种基本的加载网络图片方式,包括普通加载网络方式.用ImageLoader加载图片.用Volley加载图片. 1. [代码]普通加载网络方式 ? 1 2 3 4 5 6 7 8 9 10 ...

  9. 仿微信朋友圈图片查看-glide加载网络图片,photoview 实现缩放

    http://www.cnblogs.com/csonezp/p/5083286.html 这里实现的效果就和微信朋友圈点击图片后查看大图一样,如果你不清楚是什么效果,可以拿出手机,打开朋友圈,找到一 ...

随机推荐

  1. 使用MVC4,Ninject,EF,Moq,构建一个真实的应用电子商务SportsStore

    05 2013 档案 使用MVC4,Ninject,EF,Moq,构建一个真实的应用电子商务SportsStore(一) 摘要: 完成SportsStore电子商务平台,你将学会: 1.使用MVC4开 ...

  2. discuz的门户文章页中增加百度分享代码

    discuz虽然有百度分享插件,但是不太想用,于是自己手动添加了百度分享代码: 一.在http://share.baidu.com/地址中申请设置自己的百度分享代码,选择的风格完全按照个人喜好进行选择 ...

  3. Silverlight类百度文库在线文档阅读器(转)

    百度文库阅读器是基于Flash的,用Silverlight其实也可以做. 我实现的在线阅读器可以应用于内网文档发布,在线阅览审批等.没有过多的堆积功能,专注于核心功能.主要有以下特性: 1. 基于XP ...

  4. django下载文件

    赶快记录一下写的一个django下载文件的例子,以便以后复习: 在views.py中设置 from django.core.servers.basehttp import FileWrapper im ...

  5. 运用Unity实现AOP拦截器

    运用Unity实现AOP拦截器[结合异常记录实例] 本篇文章将通过Unity实现Aop异常记录功能:有关Unity依赖注入可以看前两篇文章: 1:运用Unity实现依赖注入[结合简单三层实例] 2:运 ...

  6. TortoiseSVN使用方法 安装和配置

    TortoiseSVN使用方法   安装和配置 TortoiseSVN的下载地址为 http://tortoisesvn.net/downloads.html 有32位和64位的版本,一定要根据自己的 ...

  7. VS2012的安装项目只能用InstallShield Limited Edition

    [吐槽]VS2012的安装项目只能用InstallShield Limited Edition[附资源下载]   以前版本的Visual Stuido中安装项目都可以使用微软自家的Visual Stu ...

  8. DotNetBar v11.4.0.6 Fully Cracked

    更新信息: http://www.devcomponents.com/customeronly/releasenotes.asp?p=dnbwf&v=11.4.0.6 如果遇到破解问题可以与我 ...

  9. Centos 64位 Install certificate on apache 即走https协议

    Centos 64位 Install certificate on apache 即走https协议 一: 先要apache 请求ssl证书的csr 一下是步骤: 重要注意事项 An Importan ...

  10. enode框架step by step之saga的思想与实现

    enode框架step by step之saga的思想与实现 enode框架系列step by step文章系列索引: 分享一个基于DDD以及事件驱动架构(EDA)的应用开发框架enode enode ...