博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 同步异步下载
阅读量:5012 次
发布时间:2019-06-12

本文共 3942 字,大约阅读时间需要 13 分钟。

 

#import "ViewController.h"

 

@interface ViewController ()<NSURLConnectionDataDelegate,NSURLConnectionDelegate>

{

    NSMutableData * myImageData;

    int num;

}

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    num =1;

 

    NSInteger a =0;

    CGFloat startPointX = (self.view.frame.size.width-20)/5;

    CGFloat startPointY = (self.view.frame.size.width-20)/5;

    for (int i=0; i<5; i++) {

        for (int j=0; j<6; j++) {

            UIImageView * image=[[UIImageView alloc]initWithFrame:CGRectMake(i*(startPointX+4)+2, j*startPointY, startPointX, startPointY)];

            image.image=[UIImage imageNamed:@"1"];

            image.tag=++a;

            [self.view addSubview:image];

            

        }

    }

    for (NSInteger i=a; i>=0; i--) {

//        UIImageView * imageview=(UIImageView*)[self.view viewWithTag:i+1];

        

//        imageview.image=[self notSameTime];

        

    }

    [self notSameTime];

    [self blocksameTime];

    // Do any additional setup after loading the view, typically from a nib.

}

#pragma mark 同步下载

-(UIImage*)SameTime

{

    NSString * str =@"http://a.hiphotos.baidu.com/exp/whcrop=90,80/sign=69220e04f703738dde1f5a60dc6b8d68/b21c8701a18b87d6f75cdabb060828381f30fd7c.jpg";

    NSURL * url =[NSURL URLWithString:str];

    NSURLRequest * request =[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f];

    //    其中缓存协议是个枚举类型包含:

    //    NSURLRequestUseProtocolCachePolicy(基础策略)

    //    NSURLRequestReloadIgnoringLocalCacheData(忽略本地缓存)

    //    NSURLRequestReturnCacheDataElseLoad(首先使用缓存,如果没有本地缓存,才从原地址下载)

    //    NSURLRequestReturnCacheDataDontLoad(使用本地缓存,从不下载,如果本地没有缓存,则请求失败,此策略多用于离线操作)

    //    NSURLRequestReloadIgnoringLocalAndRemoteCacheData(无视任何缓存策略,无论是本地的还是远程的,总是从原地址重新下载)

    //    NSURLRequestReloadRevalidatingCacheData(如果本地缓存是有效的则不下载,其他任何情况都从原地址重新下载)

    

    NSURLResponse * response=nil;

    NSError *error;

    NSData * imageData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    UIImage * image=[UIImage imageWithData:imageData];

    return image;

}

#pragma mark block异步下载

-(void)blocksameTime

{

     NSString * str =@"http://a.hiphotos.baidu.com/exp/whcrop=90,80/sign=69220e04f703738dde1f5a60dc6b8d68/b21c8701a18b87d6f75cdabb060828381f30fd7c.jpg";

    NSURL * url=[NSURL URLWithString:str];

    NSURLRequest * request=[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f];

    NSOperationQueue * que=[[NSOperationQueue alloc]init];

   [NSURLConnection sendAsynchronousRequest:request queue:que completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        UIImageView* image =(UIImageView*)[self.view viewWithTag:5];

        image.image=[UIImage imageWithData:data];

    }];

    

 

}

 

#pragma mark 异步下载

-(UIImage *)notSameTime

{

    NSString * str =@"http://a.hiphotos.baidu.com/exp/whcrop=90,80/sign=69220e04f703738dde1f5a60dc6b8d68/b21c8701a18b87d6f75cdabb060828381f30fd7c.jpg";

    NSURL * url =[NSURL URLWithString:str];

    NSURLRequest * request =[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f ];

    

 

   [NSURLConnection connectionWithRequest:request delegate:self];

        UIImage * image;

    return image;

}

//下载结束

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    UIImageView * imageview=(UIImageView*)[self.view viewWithTag:num];

    num++;

    

    imageview.image=[UIImage imageWithData:myImageData];

    myImageData=nil;

}

//下载错误

 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

    NSLog(@"发生错误=%@",error);

    

}

//加载到的数据

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

    [myImageData appendData:data];

}

请求过程中如果url包含中文

get请求中

    NSString * urlstring=@"http://c.hiphotos.baidu.com/image/pic/item/泽锋";

    urlstring=[urlstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

post 请求中需要对请求体转码

    request.HTTPBody=[@"zefeng=则分" dataUsingEncoding:NSUTF8StringEncoding];

 

 

 

 

 

 

 

 

 

 

//开始加载数据

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

    myImageData=[NSMutableData data];

    

}

转载于:https://www.cnblogs.com/xiezefeng/p/4472007.html

你可能感兴趣的文章
第二百三十一节,Bootstrap 介绍
查看>>
vi/vim 三种模式的操作
查看>>
JAVA面向对象三大特性总结
查看>>
guid
查看>>
Python中出现“TabError: inconsistent use of tabs and spaces in indentation”问题的解决
查看>>
ajax请求
查看>>
js学习总结----DOM增删改和应用
查看>>
希尔伯特矩阵(Hilbert matrix)
查看>>
(20)sopel算法
查看>>
学习总结 javascript 闭包
查看>>
实验吧一个小坑注入
查看>>
【 D3.js 高级系列 — 8.0 】 打标
查看>>
Mac必备软件推荐
查看>>
Android Gson深入分析
查看>>
display:flow-root
查看>>
判读字符串是否为空的全局宏-分享
查看>>
iOS中Block的基础用法
查看>>
mac 终端 使用ftp命令
查看>>
22-reverseString-Leetcode
查看>>
Centos 开机自动联网
查看>>