【代码笔记】iOS-HTTPQueue下载图片
一,工程图。

二,代码。
ViewController.h

#import <UIKit/UIKit.h>
#import "ASIHTTPRequest.h"
#import "ASINetworkQueue.h"
#import "NSNumber+Message.h"
#import "NSString+URLEncoding.h" @interface ViewController : UIViewController
@property (nonatomic,strong) ASINetworkQueue *networkQueue; @end

ViewController.m

//ASINetworkQueue下载图片
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//点击任何处,进行图片下载
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!_networkQueue) {
_networkQueue = [[ASINetworkQueue alloc] init];
} // 停止以前的队列
[_networkQueue cancelAllOperations]; // 创建ASI队列
[_networkQueue setDelegate:self];
[_networkQueue setRequestDidFinishSelector:@selector(requestFinished:)];
[_networkQueue setRequestDidFailSelector:@selector(requestFailed:)];
[_networkQueue setQueueDidFinishSelector:@selector(queueFinished:)]; for (int i=1; i<3; i++) {
NSString *strURL = [[NSString alloc] initWithFormat:@"http://iosbook3.com/service/download.php?email=%@&FileName=test%i.jpg",@"<你的iosbook3.com用户邮箱>",i];
NSURL *url = [NSURL URLWithString:[strURL URLEncodedString]];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; request.tag = i;
[_networkQueue addOperation:request];
} [_networkQueue go]; }
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSData *data = [request responseData];
NSError *eror;
NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&eror]; if (!resDict) {
UIImage *img = [UIImage imageWithData:data];
if (request.tag ==1) {
// _imageView1.image = img;
NSLog(@"---img--%@",img);
} else {
//_imageView2.image = img;
NSLog(@"---img--%@",img); }
} else {
NSNumber *resultCodeObj = [resDict objectForKey:@"ResultCode"]; NSString *errorStr = [resultCodeObj errorMessage];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"错误信息"
message:errorStr
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alertView show];
}
if ([_networkQueue requestsCount] == 0) {
[self setNetworkQueue:nil];
}
NSLog(@"请求成功");
} - (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"%@",[error localizedDescription]);
if ([_networkQueue requestsCount] == 0) {
[self setNetworkQueue:nil];
}
NSLog(@"请求失败");
} - (void)queueFinished:(ASIHTTPRequest *)request
{
if ([_networkQueue requestsCount] == 0) {
[self setNetworkQueue:nil];
}
NSLog(@"队列完成");
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

【代码笔记】iOS-HTTPQueue下载图片的更多相关文章
- 【代码笔记】iOS-显示图片的各种方式
代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UI ...
- 【代码笔记】iOS-清除图片缓存UIActionSheet
一,效果图. 二,代码. RootViewController.m //点击任何处出现sheet -(void)touchesBegan:(NSSet *)touches withEvent:(UIE ...
- 【代码笔记】iOS-利用图片序列创建动态图片效果
一,效果图. 二,代码. RootViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional se ...
- ios实现下载图片的裁减和显示
使用如下的方法可以裁减的同时保证了不丢失像素. - (void)connectionDidFinishLoading:(NSURLConnection *)connection{ // Set ...
- iOS下载图片失败
一.具体问题 开发的过程中,发现某个界面部分图片的显示出现了问题只显示占位图片,取出图片的url在浏览器却是能打开的,各种尝试甚至找同行的朋友帮忙在他们项目里展示都会存在问题,最终发现通过第三方框架S ...
- iOS多线程自定义operation加载图片 不重复下载图片
摘要:1:ios通过抽象类NSOperation封装了gcd,让ios的多线程变得更为简单易用: 2:耗时的操作交给子线程来完成,主线程负责ui的处理,提示用户的体验 2:自定义operati ...
- 李洪强iOS开发之 - 实现九宫格并使用SDWebImage下载图片
李洪强iOS开发之 - 实现九宫格并使用SDWebImage下载图片 源码: // // ViewController.m // 08-九宫格扩展 // // Created by 李洪强 ...
- 【转】IOS AutoLayout详解(三)用代码实现(附Demo下载)
转载自:blog.csdn.net/hello_hwc IOS SDK详解 前言: 在开发的过程中,有时候创建View没办法通过Storyboard来进行,又需要AutoLayout,这时候用代码创建 ...
- 解决iOS中tabBarItem图片默认颜色的问题(指定代码渲染模式为以原样模式的方式显示出来)
解决iOS中tabBarItem图片默认颜色的问题(指定代码渲染模式为以原样模式的方式显示出来) 解决办法:指定图片的渲染模式(imageWithRenderingMode为:UIImageRende ...
随机推荐
- 【sping揭秘】7、国际化信息支持
Spring提供messagesource接口,来进行国际化事务处理 Applicationcontext会优先找一个名为messageSouce的messageSource接口实现bean,如果找不 ...
- (转)csv — 逗号分隔值文件格式
原文:https://pythoncaff.com/docs/pymotw/csv-comma-separated-value-files/125 csv 模块主要用于处理从电子数据表格或数据库中导入 ...
- (转载)elasticsearch 查询(match和term)
原文地址:https://www.cnblogs.com/yjf512/p/4897294.html elasticsearch 查询(match和term) es中的查询请求有两种方式,一种是简易版 ...
- 关于Python数据分析与机器学习的一些资源
https://github.com/search?l=Python&o=desc&q=python&s=stars&type=Repositories&utf ...
- 全网最详细的Sublime Text 3的插件官方网站(图文详解)
不多说,直接上干货! 全网最详细的Windows里下载与安装Sublime Text *(图文详解) 全网最详细的Sublime Text 3的激活(图文详解) 全网最详细的Sublime Text ...
- Hive执行过程中出现Caused by : java.lang.ClassNotFoundException: org.cloudera.htrace.Trace的错误解决办法(图文详解)
不多说,直接上干货! 问题详情 如下 这个错误的意思是缺少 htrace-core-2.04.jar. 解决办法: 将$HBASE_HOME/lib下的htrace-core-2.04.jar拷贝到$ ...
- SQL 日期相减(间隔)datediff函数
select datediff(year, 开始日期,结束日期); --两日期间隔年 select datediff(quarter, 开始日期,结束日期); --两日期间隔季 select date ...
- 编译centos6.5:glibc 升级2.14问题
第一种:不需要 ln 创建软连接,缺点嘛,就是直接安装到系统文件夹/lib64下,想换回来就比较麻烦.(我选择的第二种,因为公司需要fpm打包,写到脚本里面,第一种之间安装在/usr目录下,打包的包安 ...
- 【K8S学习笔记】Part1:使用端口转发访问集群内的应用
本文介绍如何使用kubectl port-forward命令连接K8S集群中运行的Redis服务.这种连接方式有助于数据库的调试工作. 注意:本文针对K8S的版本号为v1.9,其他版本可能会有少许不同 ...
- 【转】CSRF基本概念
本文转自:http://www.cnblogs.com/hyddd/ 一.CSRF是什么? CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one ...