ios UIImageView异步加载网络图片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 UIImageView异步加载网络图片2的更多相关文章
- ios UIImageView异步加载网络图片
方法1:在UI线程中同步加载网络图片 UIImageView *headview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 4 ...
- UIImageView异步加载网络图片
在iOS开发过程中,经常会遇到使用UIImageView展现来自网络的图片的情况,最简单的做法如下: 去下载https://github.com/rs/SDWebImage放进你的工程里,加入头文件# ...
- android官方开源的高性能异步加载网络图片的Gridview例子
这个是我在安卓安卓巴士上看到的资料,放到这儿共享下.这个例子android官方提供的,其中讲解了如何异步加载网络图片,以及在gridview中高效率的显示图片此代码很好的解决了加载大量图片时,报OOM ...
- Libgdx实现异步加载网络图片并保存到SD卡或者data/data目录下边
Libgdx实现异步加载网络图片并保存到SD卡或者data/data目录下边,当本地有图片的时候,直接从本地读取图片,如果本地没有图片,将从服务器异步加载图片 package com.example. ...
- wemall app商城源码Android之ListView异步加载网络图片(优化缓存机制)
wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享wemall app商城源码Android之L ...
- Android批量图片加载经典系列——采用二级缓存、异步加载网络图片
一.问题描述 Android应用中经常涉及从网络中加载大量图片,为提升加载速度和效率,减少网络流量都会采用二级缓存和异步加载机制,所谓二级缓存就是通过先从内存中获取.再从文件中获取,最后才会访问网络. ...
- (BUG已修改,最优化)安卓ListView异步加载网络图片与缓存软引用图片,线程池,只加载当前屏之说明
原文:http://blog.csdn.net/java_jh/article/details/20068915 迟点出更新的.这个还有BUG.因为软引应不给力了.2.3之后 前几天的原文有一个线程管 ...
- Android批量图片加载经典系列——使用xutil框架缓存、异步加载网络图片
一.问题描述 为提高图片加载的效率,需要对图片的采用缓存和异步加载策略,编码相对比较复杂,实际上有一些优秀的框架提供了解决方案,比如近期在git上比较活跃的xutil框架 Xutil框架提供了四大模块 ...
- ios UITableView 异步加载图片并防止错位
UITableView 重用 UITableViewCell 并异步加载图片时会出现图片错乱的情况 对错位原因不明白的同学请参考我的另外一篇随笔:http://www.cnblogs.com/lesl ...
随机推荐
- 如何在 CentOS 7 中安装、配置和安全加固 FTP 服务
步骤 1:安装 FTP 服务器 1. 安装 vsftpd 服务器很直接,只要在终端运行下面的命令. # yum install vsftpd 2. 安装完成后,服务先是被禁用的,因此我们需要手动启动, ...
- eclipse+cygwin+cdt搭建c/c++开发环境
Cygwin 是一个用于 Windows 的类 UNIX shell 环境. 它由两个组件组成:一个 UNIX API 库,它模拟 UNIX 操作系统提供的许多特性:以及 Bash shell 的改写 ...
- ZooKeeper安装及配置(Windows系统下)
ZooKeeper的定义用一句话就能说清:分布式服务框架 Zookeeper -- 管理分布式环境中的数据.下面从安装开始,对这个框架进行分析. 1.安装 1. 官网下载压缩包并解压到D:\Progr ...
- linux显示桌面快捷键设置
2013-01-06 10:31:52 Ubuntu显示桌面Indicator IN: LINUX :-) HOT: 1,246 ℃ 18十2011 www.2cto.com 大家一 ...
- HDUOJ------(1230)火星A+B
火星A+B Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- VC6.0设置注释快捷键
第一步:工具栏上右键选择Customize(定制),选择“Add-ins and Macro Files(附加项和宏文件)”页,把SAMPLE前面打上钩. 第二步:选择“Commands(命令)”页, ...
- Oracle SQL Developer,Oracle 开发工具之toad、SQL Developer、PL/SQL Developer等比较
参考: oracle 的几个开发工具比较 因Oracle几乎是中大型商业企业数据的首选,所以比较一下常用与Oracle的工具. Oracle SQL Developer 免费,一般开发使用足矣,常用. ...
- 微信小程序四(设置底部导航)
好了 小程序的头部标题 设置好了,我们来说说底部导航栏是如何实现的. 我们先来看个效果图 这里,我们添加了三个导航图标,因为我们有三个页面,微信小程序最多能加5个. 那他们是怎么出现怎么着色的呢?两步 ...
- Quartz.net官方开发指南[转]
http://www.cnblogs.com/shanyou/category/102991.html
- JFinal record类型数据在前台获取
1.jfinal record还得自己处理一下 可以使用 this.setSessionAttr("user", record.getColumns()); 这样在jsp中el表达 ...