ViewController.h
1 #import <UIKit/UIKit.h> 2 #import "ASIHTTPRequest.h" 3 4 @interface ViewController : UIViewController 5 <ASIHTTPRequestDelegate> 6 { 7 UIImageView* imageView; 8 } 9 10 @end
ViewController.m
1 #import "ViewController.h" 2 3 @implementation ViewController 4 5 #pragma mark - viewDidLoad Event 6 -(void)initImageViewF 7 { 8 imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 240)]; 9 [self.view addSubview:imageView]; 10 [imageView release]; 11 } 12 -(void)startDownloadF 13 { 14 NSString* str = @"http://192.168.88.8/sns/my/headimage.php?uid=865"; 15 NSURL* url = [NSURL URLWithString:str]; 16 17 ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL:url]; 18 request.tag = 10; 19 request.delegate = self; 20 //开始异步下载 21 [request startAsynchronous]; 22 } 23 24 #pragma mark - viewDidLoad 25 - (void)viewDidLoad 26 { 27 [super viewDidLoad]; 28 //初始.h里的字段 29 [self initImageViewF]; 30 //开始下载 31 [self startDownloadF]; 32 33 } 34 35 #pragma mark - ASIHTTPRequestDelegate 36 //下载完成 37 - (void)requestFinished:(ASIHTTPRequest *)request{ 38 if (request.tag == 10) { 39 40 } 41 //如果下载的内容是字符串 42 //NSString* str = request.responseString; 43 //如果下载的是数据 44 NSData* data = request.responseData; 45 UIImage* image = [UIImage imageWithData:data]; 46 imageView.image = image; 47 } 48 //下载失败 49 - (void)requestFailed:(ASIHTTPRequest *)request{ 50 NSLog(@"下载失败"); 51 }