UI 网络程序

时间:2022-09-20 22:34:31

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

-(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. 使用Code::blocks在windows下写网络程序

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

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

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

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

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

  4. C&num; 为网络程序添加用户代理

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

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

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

  6. 如何编写一个稳定的网络程序&lpar;TCP&rpar;

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

  7. Android&colon; 网络随时需要在3G和Wifi切换,网络程序需要注意

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

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

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

  9. 基于LPCXpresso54608开发板创建Embedded Wizard UI应用程序

    平台集成和构建开发环境:LPCXpresso 54608入门指南 本文主要介绍了创建一个适用于LPCXpresso54608开发板的Embedded Wizard UI应用程序所需的所有必要步骤.请一 ...

随机推荐

  1. c&num;与java的区别

    经常有人问这种问题,用了些时间java之后,发现这俩玩意除了一小部分壳子长的还有能稍微凑合上,基本上没什么相似之处,可以说也就是马甲层面上的相似吧,还是比较短的马甲... 一般C#多用于业务系统的开发 ...

  2. vim黏贴代码格式混乱的解决方法

    from:http://blog.csdn.net/commshare/article/details/6215088 感谢牛人的文章.解决了我在vim使用中,很头疼的问题. 在vim新建文件的时候, ...

  3. 【翻译习作】 Windows Workflow Foundation程序开发-第一章05

    1.3      开发我们的第一个工作流 也许你曾经在这样的产品经理手下搞过开发:他总是在你身边转悠,并不时的问一句“你还没做完吗?”.在这一部分,我们将用一个简单的Windows Workflow程 ...

  4. Memcache&comma;Redis

    Memcache Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度. ...

  5. &commat;Data 注解引出的 lombok 小辣椒

    今天在看代码的时候, 看到了这个注解, 之前都没有见过, 所以就查了下, 发现还是个不错的注解, 可以让代码更加简洁. 这个注解来自于 lombok,lombok 能够减少大量的模板代码,减少了在使用 ...

  6. 目标检测 非极大值抑制(Non-Maximum Suppression,NMS)

    非极大值抑制(Non-Maximum Suppression,NMS),顾名思义就是抑制不是极大值的元素,可以理解为局部最大搜索.也可以理解为只取置信度最高的一个识别结果. 举例:  如图所示,现在 ...

  7. 关于怎样获取DevExpress GridView过滤后或排序后的数据集问题&lpar;转&rpar;

    GridView用自带的过滤功能过滤数据后,想要获取过滤后的数据集,有两种方式: 一.笨办法就是循环遍历GridView,根据gridView.GetRow()或者gridView.GetDataRo ...

  8. myBatis学习之路1-基本功能实现

    myBatis也是一个持久型框架,相较于hibernate来说,算是轻量级的. 1.配置mybatis环境 相关jar下载地址:mybatis+mysalJAR包 2.新建一个java project ...

  9. jquery之find&comma;filter&comma;has对比

    find()方法找的是符合条件的后代,返回的是子元素. $('div').find('.intro').css('color','red'); //寻找div后代类为intro的元素 filter() ...

  10. (BestCoder Round &num;64 &lpar;div&period;2&rpar;)Array

    BestCoder Round #64 (div.2) Array 问题描述 Vicky是个热爱数学的魔法师,拥有复制创造的能力. 一开始他拥有一个数列{1}.每过一天,他将他当天的数列复制一遍,放在 ...