【代码笔记】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 ...
随机推荐
- Mac系统安装和卸载brew包管理
brew 的官网地址 https://brew.sh/ 1.brew的安装 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercon ...
- MyEclipse 编写 JSP 代码时很卡的解决办法
在网上看到很多方法,都是尝试过,个人感觉都没有说到重点,所以收效甚微. 后来自己总结了一下: 我们都是习惯在MyEclipse 工具,双击jsp 文件打开进行编辑.这时,工具会打开窗口的 Previe ...
- Scala使用隐式转换进行比较
Boy.scala class Boy(val name: String, val faceValue: Int) extends Comparable[Boy]{ override def comp ...
- 01-SpringBoot项目:helloworld
1.Spring 官网:spring.io 2.继承SpringBoot的父项目 <parent> <groupId>org.springframework.boot</ ...
- Linux添加防火墙、iptables的安装和配置(亲测)
iptables基础 规则(rules)其实就是网络管理员预定义的条件,规则一般的定义为“如果数据包头符合这样的条件,就这样处理这个数据包”.规则存储在内核空间的信息 包过滤表中,这些规则分别指定了源 ...
- <Think Python>中统计文献单词的处理代码
def process_line(line, hist): """Adds the words in the line to the histogram. Modi ...
- Java NIO系列教程(七) FileChannel
Java NIO中的FileChannel是一个连接到文件的通道.可以通过文件通道读写文件. FileChannel无法设置为非阻塞模式,它总是运行在阻塞模式下. 打开FileChannel 在使用F ...
- Hibernate关联关系映射之一对多双向映射
一对多映射有两种,一种是单向的,另一种的多向.我们一般是使用双向的,所以我就写写一对多的双向映射. 还是想昨天一样举个例子来说明:作者<===>作品,还是对数据进行增删改查. 我们一般是把 ...
- let‘s encrypt之nginx-https没有小锁
1.使用let's encrypt 加密后的nginx,访问的时候,发现没有小锁,chrome中: 火狐浏览器上: 会看到如上信息,这是因为css.图片或javascript任然通过http提供,可以 ...
- git 分支 branch 操作
创建分支 git branch test: 基于当前commit创建test分支..git/HEAD 文件中记录了当前分支名字. 删除分支 git branch -d test:删除本地test分支 ...