一,从网络地址获取一张图片

-(void)didClickDownLoad:(id)sender
{
    NSLog(@"%@",[NSDate date].description);
    NSURL *url=[NSURL URLWithString:@"http://124.205.1.1/student/class_12/team_learn/a.png"];
    
     NSURLRequest *request=[NSURLRequest requestWithURL:url];
  // NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];
    
    //NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
   [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
       
       //将data转换成image对象
       // UIImage *image=[[UIImage alloc]initWithData:data];
       UIImage *image=[UIImage imageWithData:data];
       self.imageView.image=image;
       self.imageView.backgroundColor=[UIColor clearColor];
       self.imageView.contentMode=UIViewContentModeScaleAspectFit;
    }];
   
}

 

二,访问服务器,计算结果输出,post

- (void)viewDidLoad
{
    [super viewDidLoad];
//    构造网络地址对象
     NSURL *url=[NSURL URLWithString:@"http://124.205.147.26/student/class_12/team_learn/lichanghong.php"];
    //
    //构造复杂网络请求对象
    // NSURLRequest *request=[NSURLRequest requestWithURL:url];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    //修改网络请求使用的方法,默认是get
    [request setHTTPMethod:@"post"];
    //设置网络请求中包含的信息
    [request setHTTPBody:[@"first_value=1&second_value=2" dataUsingEncoding:NSUTF8StringEncoding]];
    //发送同步网络请求
    NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    //将data装换为字符串对象
    NSString *content=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",content);
    // Do any additional setup after loading the view, typically from a nib.
}

 

//--------以上为所有内容,可以以不变应万变---下面内容为练习

自定义一个tableView的cell文件,里面有一个属性@property (nonatomic,strong)UIImageView *imgView;

此处xml文档的解析用GDataXMLNode第三方类

主要功能:获得网络文件中图片到tableview中

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.imageArray=[NSMutableArray array];
    //获得图片个数来确定行数
    NSURL *url=[NSURL URLWithString:@"http://124.205.147.26/student/class_12/team_homework/count.php"];
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    imageCount=[[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding] integerValue];
    
    //tableView的设置
    UITableView *tableView=[[UITableView alloc]initWithFrame:self.view.bounds];
    tableView.dataSource=self;
    tableView.rowHeight=300;
    [self.view addSubview:tableView];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)layoutSublayersOfLayer:(CALayer *)layer
{

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return imageCount;
}

-(CustomTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    static NSString *identifier=@"cell";
    CustomTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell=[[CustomTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
   
    
    
    //------------------获取图片地址------------
    //获取存放图片的xml文件
    NSString *str=[NSString stringWithFormat:@"http://124.205.147.26/student/class_12/team_homework/image.php?image_id=%i",indexPath.row+1];
    NSURL *url2=[NSURL URLWithString:str];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url2];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        //解析xml
        GDataXMLDocument *doc=[[GDataXMLDocument alloc]initWithData:data options:0 error:Nil];
        GDataXMLElement *root=doc.rootElement;
        GDataXMLElement *imgNode=[[root elementsForName:@"url"]lastObject];
        
        NSURL *url=[NSURL URLWithString:imgNode.stringValue];
        NSMutableURLRequest *mrequest=[NSMutableURLRequest requestWithURL:url];
        
        [NSURLConnection sendAsynchronousRequest:mrequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            UIImage *img=[UIImage imageWithData:data];
            cell.imgView.image=img;
            [cell.imgView sizeToFit];
        }];
        
    }];
    
        return cell;
}