编写一个如下界面,实现:

1、在文本输入框中输入一个网址,然后点击显示图片,图片显示到UIImageView中。

2、点击下载,这张显示的图片被下载到手机的Documents文件夹下的Dowmload目录下,并按序号命名。

3、在文本框输入完成之后点击其他地方,键盘自动消失。

准备工作:

1、输入的URL有可能是http而非https,需要在Info.plist中添加如下代码:

    <key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

添加之后,Info.plist如下:

接下来是代码,代码全部写在UIViewController的.m文件中:

 #import "ViewController.h"

 @interface ViewController (){
// 定于全局变量
UIButton *btnDownLoad;
UIButton *btndisplay;
UIImageView *imageView;
UITextField *textFieldinputUrl;
NSData *imagedata;
NSString *newpath;
int count; } @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // 显示图片按钮初始化及加载
// 初始化为圆角矩形按钮
btndisplay = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// 设置按钮的位置,大小
btndisplay.frame = CGRectMake(, , , );
// 设置按钮背景色
btndisplay.backgroundColor = [UIColor colorWithRed:0.512 green:0.562 blue:0.943 alpha:1.000];
// 设置按钮文本颜色及文本内容
[btndisplay setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btndisplay setTitle:@"显示图片" forState:UIControlStateNormal] ;
// 给按钮添加显示图片事件
[btndisplay addTarget:self action:@selector(display:) forControlEvents:UIControlEventTouchUpInside];
// 把按钮加载到view上
[self.view addSubview:btndisplay]; // 下载图片按钮初始化及加载
btnDownLoad = [[UIButton alloc]initWithFrame:CGRectMake(, , , )];
btnDownLoad.backgroundColor = [UIColor colorWithRed:0.512 green:0.562 blue:0.943 alpha:1.000];
[btnDownLoad setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnDownLoad setTitle:@"下载图片" forState:UIControlStateNormal] ;
[btnDownLoad addTarget:self action:@selector(download:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnDownLoad]; // 设置URL输入框
// 初始化文本输入框及背景色设置
textFieldinputUrl = [[UITextField alloc]initWithFrame:CGRectMake(, , , )];
textFieldinputUrl.backgroundColor = [UIColor colorWithRed:0.653 green:1.000 blue:0.919 alpha:1.000];
// 设置文本输入前提示水印
textFieldinputUrl.placeholder = @"请输入图片地址";
// 设置文本输入框中消除按钮
textFieldinputUrl.clearButtonMode = UITextFieldViewModeAlways;
// 把文本输入框加载到View
[self.view addSubview:textFieldinputUrl]; // 设置图片控件
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(, , , )];
imageView.backgroundColor = [UIColor grayColor];
[self.view addSubview:imageView]; // 获取沙盒中document的路径
NSString *dictpath = [NSString stringWithFormat:@"%@/Documents",NSHomeDirectory()];
// 创造一个字符串指向document中的要新建的download文件夹
newpath =[NSString stringWithFormat:@"%@/download",dictpath];
// 在document中新建download文件夹
[[NSFileManager defaultManager]createDirectoryAtPath:newpath withIntermediateDirectories:YES attributes:nil error:nil]; NSLog(@"%@",dictpath);
} //显示图片
-(void)display:(id)sender{ // 把文本框输入的URL转换成数据
NSURL *imageurl = [NSURL URLWithString:textFieldinputUrl.text];
imagedata = [NSData dataWithContentsOfURL:imageurl];
// 把数据转换成图片
UIImage *image = [UIImage imageWithData:imagedata];
// 转换的图片给UIImageView显示
imageView.image =image; } //下载图片
-(void)download:(id)sender{
count++;
// 在路径下新建图片文件
NSString *newfilepath = [NSString stringWithFormat:@"%@/%d.jpg",newpath,count];
// 把图片数据保存在路径下新建好的文件中
[[NSFileManager defaultManager]createFileAtPath:newfilepath contents:imagedata attributes:nil];
} //输入结束后让键盘滚蛋
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[textFieldinputUrl resignFirstResponder];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

最终输入URL点击显示图片之后显示:

点击下载图片之后,在手机中本软件的路径下:

需要注意的地方有:

1、文本框在输入完后,我们点击文本框之外的其他地方,键盘如何消失:

 //输入结束后让键盘滚蛋
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[textFieldinputUrl resignFirstResponder];
}

2、下载图片时如何自动为图片命名:定义一个全局变量用来计算点击下载图片按钮的次数,然后根据次数为图片命名,可保证不重名。然后生成格式化字符串作为下载后图片的名字:

 //下载图片
-(void)download:(id)sender{
count++;
// 在路径下新建图片文件
看集 NSString *newfilepath = [NSString stringWithFormat:@"%@/%d.jpg",newpath,count];
// 把图片数据保存在路径下新建好的文件中
[[NSFileManager defaultManager]createFileAtPath:newfilepath contents:imagedata attributes:nil];
}

IOS开发-UI学习-根据URL显示图片,下载图片的练习(button,textfield,image view,url,data)的更多相关文章

  1. 李洪强iOS开发之 - 实现九宫格并使用SDWebImage下载图片

     李洪强iOS开发之 - 实现九宫格并使用SDWebImage下载图片  源码:  // //  ViewController.m //  08-九宫格扩展 // //  Created by 李洪强 ...

  2. iOS开发——UI基础-自定义构造方法,layoutSubviews,Xib文件,利用Xib自定义View

    一.自定义构造方法 有时候需要快速创建对象,可以自定义构造方法 + (instancetype)shopView { return [[self alloc] init]; } - (instance ...

  3. iOS开发UI篇—UIScrollView控件实现图片缩放功能

    iOS开发UI篇—UIScrollView控件实现图片缩放功能 一.缩放 1.简单说明: 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对 ...

  4. iOS开发UI篇—Quartz2D使用(图片剪切)

    iOS开发UI篇—Quartz2D使用(图片剪切) 一.使用Quartz2D完成图片剪切 1.把图片显示在自定义的view中 先把图片绘制到view上.按照原始大小,把图片绘制到一个点上. 代码: - ...

  5. 【转】 iOS开发UI篇—UIScrollView控件实现图片轮播

    原文:http://www.cnblogs.com/wendingding/p/3763527.html iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播 ...

  6. iOS开发UI篇—UIScrollView控件实现图片轮播

    iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播            二.实现代码 storyboard中布局 代码: #import "YYV ...

  7. iOS开发UI篇—CAlayer(创建图层)

    iOS开发UI篇—CAlayer(创建图层) 一.添加一个图层 添加图层的步骤: 1.创建layer 2.设置layer的属性(设置了颜色,bounds才能显示出来) 3.将layer添加到界面上(控 ...

  8. iOS开发UI篇—CALayer简介

    iOS开发UI篇—CALayer简介   一.简单介绍 在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView. 其实 ...

  9. iOS开发UI篇—核心动画(UIView封装动画)

    iOS开发UI篇—核心动画(UIView封装动画) 一.UIView动画(首尾) 1.简单说明 UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画 ...

  10. ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局

    本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...

随机推荐

  1. 本地php 连接 MySQL

    1. 在D:\xampp\htdocs下创建test.php <?php $dbhost = 'localhost:3306'; //mysql服务器主机地址 $dbuser = 'root'; ...

  2. hadoop在子节点上没有datanode进程

    经常会有这样的事情发生:在主节点上start-all.sh后,子节点有TaskTracker进程,而没有DataNode进程.环境:1NameNode   2DataNode三台机器,Hadoop为1 ...

  3. android 学习之RecyclerView

    RecyclerView:ListView的升级版,它提供了更好的性能而且更容易使用.该控件是一个可以装载大量的视图集合,并且可以非常效率的进行回收和滚动.当你list中的元素经常动态改变时可以使用R ...

  4. 转:WebDriver(Selenium2) 判断页面是否刷新的方法

    public static boolean waitPageRefresh(WebElement trigger) { int refreshTime = 0; boolean isRefresh = ...

  5. 百度地图移动版API 1.2.2版本(Android)地图偏移的最佳解决办法

    Import import com.baidu.mapapi.CoordinateConvert;import com.baidu.mapapi.GeoPoint; Code GeoPoint p = ...

  6. 使用curl获取乱码问题

    今天通过curl获取百度地图接口数据,获取到居然是乱码,于是我查看是不是编码问题,发现返回的编码和自己的编码都是utf-8, 继续找原因,发现header报文中  Content-encoding 为 ...

  7. g1gc

    http://www.infoq.com/articles/G1-One-Garbage-Collector-To-Rule-Them-All http://blog.csdn.net/renfufe ...

  8. 8数码,欺我太甚!<bfs+康拓展开>

    不多述,直接上代码,至于康拓展开,以前的文章里有 #include<iostream> #include<cstdio> #include<queue> using ...

  9. Fragment(四)Fragment生命周期分析(转)

    Fragment(四)Fragment生命周期分析 转载请注明:http://blog.csdn.net/liaoqianchuan00/article/details/24271607   例子一 ...

  10. iptables-过滤61开头的ip数据包

    iptables -F root@android:/system/bin # iptables -L -n iptables -L -n Chain INPUT (policy ACCEPT) tar ...