[iphone]网络请求

- (IBAction)loadDataAction:(id)sender {
    NSString * urlString = @"http://farm9.staticflickr.com/8081/8416842962_e2f4e28925_m.jpg";
    NSURL * url = [NSURL URLWithString:urlString];
    NSURLRequest * request = [[NSURLRequest alloc]initWithURL:url];
    NSURLConnection* connection1 = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    self.connection = connection1;
}

.h文件  继承

<NSURLConnectionDelegate>

实现两个方法:

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data1{
    if (!data)
        self.data = [NSMutableData alloc];
    [self.data appendData:data1];
}
-(void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
    UIImage * img = [UIImage imageWithData:self.data
                     ];
    [self.loadBtn setBackgroundImage:img forState:UIControlStateNormal];
}


你可能感兴趣的:(iphone)