//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. nhibernate+autofac+mvc的demo

    想自己做一个小的demo.目的是能够提供一个系统架构,在这个基础上,可以快速开发一些小型的系统.

  2. 使用WCF扩展在方法调用前初始化环境

    使用WCF扩展在方法调用前初始化环境 OperationInvoker 介绍 OperationInvoker 是 WCF 运行时模型中在调用最终用户代码前的最后一个扩展点,OperationInvo ...

  3. TOGAF架构能力框架之架构合同、成熟度模型和架构技能框架

    TOGAF架构能力框架之架构合同.成熟度模型和架构技能框架 5. 架构合同 架构合同是在开发团体和赞助者之间关于架构的交付物.质量以及适用目标的联合协议,并且通过有效的架构治理将会促使这些协议的成功施 ...

  4. mac os apache 配置方法详细介绍

    我使用的Mac OS X版本是10.8.2,Mac自带了Apache环境. 启动Apache 设置虚拟主机 启动Apache 打开“终端(terminal)”,输入 sudo apachectl -v ...

  5. hdu1686

    题目意思:找到上串在下串中有多少个 Problem Description The French author Georges Perec (1936–1982) once wrote a book, ...

  6. Sublime Text 3设置笔记

    Sublime Text 3设置笔记 Sublime Text 3设置指南 1. 安装package control 下载package control源码安装包,并解压: http://yun.ba ...

  7. A First Look at Rust Language

    文 Akisann@CNblogs / zhaihj@Github 本篇文章同时发布在Github上:http://zhaihj.github.io/a-first-look-at-rust.html ...

  8. MBR,boot loader, partition table, backup, recovery, clean 硬盘引导记录,分区表备份,恢复,清空

    linux/unix系统的分区表结构位于硬盘开始的MBR上.MBR上大小为512bytes. MBR=446B启动信息+64B分区表+2B分隔符=512字节 前面的446bytes作为启动信息,启动信 ...

  9. Data Guard组件等相关介绍

    1.Data Guard组件介绍  Data Guard架构归类为3个主要的组件. Data Guard 重做传输服务  重做传输服务用来将主数据库生成的重做数据传输给备用数据库. Data Guar ...

  10. unix网络io模型

    阻塞I/O(bloking I/O) 阻塞IO的特点就是在IO执行的两个阶段(recvfrom和数据从内核空间转移到用户空间)都被block了 非阻塞I/O(non-bloking I/O)   非阻 ...