HttpRequest网络编程的实现框架

在上篇所建立的网络编程框架上,怎么下载数据呢?下面以下载图片为例。
首先创建一个UIViewcontrollor

#import "baseViewController.h"

#import "HttpRequest.h"

#define URL_IMAGE @"http://www.imanhua.com/Cover/2013-05/ybhtdn.jpg"//图片网址

@interface oneViewController : UIViewcontrollor<HTTPRequestDownDelegate>//实现上篇所写的网络编程协议

{

   NSMutableArray * m_allTitle;

    NSStringEncoding enc;

       NSMutableDictionary *data_dic;

}

@end


以下是.m 文件

#import "oneViewController.h"


@interface oneViewController ()

@property (nonatomic,retain) UIView *container; //用来接收下载的图片

@end


@implementation oneViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

- (void)viewDidLoad

{

    [super viewDidLoad];

     [self buildLayout];

// Do any additional setup after loading the view.

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

-(void)setImage:(NSData *)data

{

    UIImage *image = [[UIImage alloc] initWithData:data];

    UIImageView *view = [[UIImageView alloc] initWithImage:image];

    view.frame = self.container.bounds;

    //设置UIView的包含模式,自动调整子view到合适的大小

    view.contentMode = UIViewContentModeScaleAspectFit;

    [self.container addSubview:view];

}

-(void)clearContainer

{

    [self.container.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

}

-(void)buildLayout

{

    self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];

    [self clearContainer];

    HttpRequest *demo = [[HttpRequest alloc] init];

    demo.delegate = self;

    [demo downLoadFromURL:URL_IMAGE];

    [self.view addSubview:self.container];

 }

-(void)complateWithData:(NSData *)data

{

    [self setImage:data];

}

//如果下载失败,则回调该方法

-(void)failedWithError:(NSError *)error

{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"出错啦~" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];

    [alert show];

}

-(UIView *)container

{

    if (!_container) {

        _container = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 260, 300)];

        _container.backgroundColor = [UIColor whiteColor];

    }

    return _container;

}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值