• 【代码笔记】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
    复制代码
  • 相关阅读:
    Julia
    《风控策略笔记》之风控审批策略(三)--量化指标与策略调优
    《风控策略笔记》之风控审批策略(二)--决策引擎与策略调优
    《风控策略笔记》之风控审批策略(一)--前言与审批策略架构搭建和数据源
    schannel: next InitializeSecurityContext failed: SEC_E_ILLEGAL_MESSAGE错误
    从 kswapd0 进程CPU占用过高 到计算机内存详解
    pandas窗口函数--rolling
    请求行与相应行
    URI与URN与URL详解
    mysql索引详解
  • 原文地址:https://www.cnblogs.com/yang-guang-girl/p/7894497.html
Copyright © 2020-2023  润新知