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

-(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;
}

UI 网络程序的更多相关文章

  1. Docker容器:将带UI的程序直接转为Web应用,so easy

    摘要:使用Docker容器,将带UI的程序,直接转换为Web应用.很方便,跟大家分享一下. 本文分享自华为云社区<使用Docker容器,将带UI的程序,直接转为Web应用>,作者:tsjs ...

  2. 使用Code::blocks在windows下写网络程序

    使用Code::blocks在windows下写网络程序 作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创 ...

  3. iOS开发UI篇—程序启动原理和UIApplication

    iOS开发UI篇—程序启动原理和UIApplication   一.UIApplication 1.简单介绍 (1)UIApplication对象是应用程序的象征,一个UIApplication对象就 ...

  4. 开源一个基于nio的java网络程序

    因为最近要从公司离职,害怕用nio写的网络程序没有人能看懂(或许是因为写的不好吧),就调整成了mina(这样大家接触起来非常方便,即使没有socket基础,用起来也不难),所以之前基于nio写的网络程 ...

  5. C# 为网络程序添加用户代理

    如何为网络程序添加用户代理,本人推荐使用UrlMkSetSessionOption函数, 不过该函数有一个弱点不可以X64编译,X86编译软件才可以调用该函数 否则会一直返回!S_OK意义错误.第二呢 ...

  6. iOS开发UI篇—程序启动原理和UIApplication1

    iOS开发UI篇—程序启动原理和UIApplication   一.UIApplication 1.简单介绍 (1)UIApplication对象是应用程序的象征,一个UIApplication对象就 ...

  7. 如何编写一个稳定的网络程序(TCP)

    本节我们看一下怎样才能编写一个基于TCP稳定的客户端或者服务器程序,主要以试验抓包的方式观察数据包的变化,对网络中出现的多种情况进行分析,分析网络程序中常用的技术及它们出现的原因,在之后的编程中能早一 ...

  8. Android: 网络随时需要在3G和Wifi切换,网络程序需要注意

    平时,3G和WIFI 都开着的时候,Android默认使用Wifi,但现实环境中不可能到处都有wifi,所以手机会经常自动切换网络. 有的时候,手机一开始使用wifi上网,当进入待机后10-30分钟, ...

  9. udp网络程序-发送、接收数据

    1. udp网络程序-发送数据 创建一个基于udp的网络程序流程很简单,具体步骤如下: 创建客户端套接字 发送/接收数据 关闭套接字 代码如下: #coding=utf-8from socket im ...

随机推荐

  1. arrow css

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. js构建工具和预编译

    Gulp应该和Grunt比较,他们的区别我就不说了,说说用处吧.Gulp / Grunt 是一种工具,能够优化前端工作流程.比如自动刷新页面.combo.压缩css.js.编译less等等.简单来说, ...

  3. css改变图片的颜色

    参考大神张鑫旭:http://www.zhangxinxu.com/wordpress/2016/06/png-icon-change-color-by-css/ 主要知识点:border-right ...

  4. C#入门篇6-10:字符串操作 DateTime操作

    #region DateTime操作 public class C3 { //DateTime常用的操作 public static void Fun1() { //格式:2012-8-16 11:2 ...

  5. sqlserver 2008 存储过程调用存储过程或方法

    函数:拆分字符串,并返回一个table CREATE FUNCTION [dbo].[f_splitSTR](@s varchar(max), --待分拆的字符串@split varchar(10) ...

  6. 使用Apache HttpClient

    在一般情况下,如果只是需要Web站点的某个简单页面提交请求并获取服务器响应,完全可以使用前面所介绍的HttpConnection来完成.但在绝大部分情况下,Web站点的网页可能没这么简单,这些页面并不 ...

  7. 牛场围栏(vijos 1054)

    题目大意: 给出N种木棍(每种木棍数量无限)的长度(<=3000),每根木棍可以把它切掉[1,M]的长度来得到新的木棍. 求最大的不能被组合出来的长度. 如果任何长度都能组合出来或者最大值没有上 ...

  8. java.io中流的操作:字节流、字符流

    java.io中流的操作:字节流.字符流(1)使用File类打开一个文件(2)通过字节流或字符流的子类指定输出的位置(3)进行读/写操作(4)关闭输入/输出 1.字节流:主要是byte类型数据,以by ...

  9. [转载]android的消息处理机制(图+源码分析)——Looper,Handler,Message

    2013-12-18 14:17:33 转载自: http://www.cnblogs.com/codingmyworld/archive/2011/09/14/2174255.html 请跳转到转载 ...

  10. NOIP 2013提高组day 1 T 1转圈游戏 快速幂

    描述 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号位置,……,依此类推 ...