#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