UIImageView是我们做iOS开发用的非常多的一个控件,IOS中的各种图片,包括头像,有的背景图片等基本都要用到这个控件。

1、常用的属性以及方法

<span style="font-size:14px;">// 初始化图片
- (id)initWithImage:(UIImage *)image;
// 初始化带高亮的图片
- (id)initWithImage:(UIImage *)image highlightedImage:(UIImage
*)highlightedImage
// 点语法设置图片
@property(nonatomic,retain) UIImage *image;
// 点语法设置高亮图片
@property(nonatomic,retain) UIImage *highlightedImage
// 是否打开用户交互,默认为NO
@property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;
@property(nonatomic) UIViewContentMode contentMode;</span>

2、UIImageView的技术点

UIImageView常用属性就这么多,但是UIImageView的技术点确很多,比如contentMode,这个属性是继承自父类UIView的,但是这个枚举类型中三个类型是专门为UIImageView使用的,用来处理图片的拉伸方式。对应的拉伸效果如下:

有的时候,我们需要给UIImageView加上点击事件,这时候我们有两种方式来实现这个功能。首先两种方式都需要设置userInteractionEnabled为YES,因为UIImageView的这个属性默认为NO,其实UILabel也是默认为NO的。

方法一:给UIImageView加上手势

示例代码如下:

<span style="font-size:14px;">UIImageView *imageView1 = [[UIImage alloc] initWithFrame:CGRectMake(110,20,100,100)];
imageView1.userInteractionEnabled = YES;
imageView1.backgroundColor = [UIColor clearColor];
imageView1.image = [UIImage imageNamed:@"love.png"];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] intiWithTarget:self action:@selector(tapAction)];
[imageView1 addGestureRecognizer:tapGesture];
[tapGesture release];
[self.view addSubView:imageView1];
[imageView1 release];</span>

上面代码是先创建了一个UIImageView,然后给其绑定了一个tap手势,通过实现手势的方法来处理点击事件。

方法二:在UIImageView上添加一个自定义的透明的Button(或者干脆就用button来实现这个功能)

<span style="font-size:14px;">UIImageView *imageView1 = [[UIImage alloc] initWithFrame:CGRectMake(110,20,100,100)];
imageView1.userInteractionEnabled = YES;
imageView1.backgroundColor = [UIColor clearColor];
imageView1.image = [UIImage imageNamed:@"love.png"];
UIButton *button = [[UIButton alloc] initWithFrame:imageView1.bounds];
[button addTarget:self action:@selector(clickAction) forControlEvents:UIControlEventTouchUpInside];
[imageView1 addSubView:button];
[button release];
[imageView1 release];</span>

有时候我们需要对一个图片进行处理,也会有一个常用的方法:

<span style="font-size:14px;">- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;</span>

该方法第一个参数是拉伸距离原点的横向距离,第二个参数是纵向距离距离,会将一个图片拉伸处理后返回一个新的图片。



还有一个技术点,有的时候,我们需要加载网络图片,不是app里的资源图片,这时候怎么处理呢?UIImageView是不可以加载网络图片的。

我们可以利用UIImage的imageWithData这个方法,但是这个方法会阻塞主线程,示例代码如下:

<span style="font-size:14px;">UIImageView *imageView1 = [[UIImage alloc] initWithFrame:CGRectMake(110,20,100,100)];
imageView1.userInteractionEnabled = YES;
imageView1.backgroundColor = [UIColor clearColor];
NSString *imagePath = @"http://www.baidu.com/xxxxx.png";
NSData *data = [NSData dataWithContentsOfURL:[[NSURL alloc] initWithString:imagePath]];
imageView1.image = [UIImage imageWithData:data];
[imageView1 release];</span>

其实我们可以用第三方的框架来实现,这个框架叫SDWebImage,它对UIImageView添加了category方法,可以直接异步加载一个NSURL。

<span style="font-size:14px;">UIImageView *imageView1 = [[UIImage alloc] initWithFrame:CGRectMake(110,20,100,100)];
imageView1.userInteractionEnabled = YES;
imageView1.backgroundColor = [UIColor clearColor];
NSString *imagePath = @"http://www.baidu.com/xxxxx.png";
//加载网络图片数据
[_image setImageWithURL:[NSURL URLWithString:*imagePath ]];
[imageView1 release];</span>

最后,补充一点,UIImageView可以播放多张图片。代码如下:

<span style="font-size:14px;">UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(160-41/2.0, 100, 100, 150)];
imgView.animationImages = @[[UIImage imageNamed:@"img_two.jpg"],[UIImage imageNamed:@"img_three.jpg"]];
imgView.animationDuration = 2;
[imgView startAnimating];</span>

上面的animationDuration是动画持续时间,就是两张图片一共的播放时间,如果是4张图片,就是每张播放播放0.5秒的样子。

还有一个animationRepeatCount是设置重复次数,默认是0次,无限重复。

iOS开发之六:常用控件--UIImageView的使用的更多相关文章

  1. IOS开发自定义CheckBox控件

    IOS本身没有系统的CheckBox组件,但是实际开发中会经常用到,所以专门写了一个CheckBox控件,直接上代码 效果图: UICheckBoxButton.h文件如下: #import #imp ...

  2. IOS开发中设置控件内容对齐方式时容易混淆的几个属性

    IOS开发中四个容易混淆的属性: 1. textAligment : 文字的水平方向的对齐方式 1> 取值 NSTextAlignmentLeft      = 0,    // 左对齐 NST ...

  3. ios开发中button控件的属性及常见问题

    最为最基本的控件,我们必须对button的每个常用属性都熟练应用: 1,使用之前,必须对按钮进行定义,为乐规范,在@interface ViewController (){}中进行定义,先定义后使用. ...

  4. iOS 开发 ZFUI framework控件,使布局更简单

    来自:http://www.jianshu.com/p/bcf86b170d9c 前言 为什么会写这个?因为在iOS开发中,界面的布局一直没有Android布局有那么多的方法和优势,我个人开发都是纯代 ...

  5. iOS开发基础-UITableView控件简单介绍

     UITableView 继承自 UIScrollView ,用于实现表格数据展示,支持垂直滚动.  UITableView 需要一个数据源来显示数据,并向数据源查询一共有多少行数据以及每一行显示什么 ...

  6. iOS开发中UIDatePicker控件的使用方法简介

    iOS上的选择时间日期的控件是这样的,左边是时间和日期混合,右边是单纯的日期模式. 您可以选择自己需要的模式,Time, Date,Date and Time  , Count Down Timer四 ...

  7. iOS开发无第三方控件的援助达到的效果侧边栏

    最近的研究iOS程序侧边栏.渐渐的发现iOS该方案还开始采取风侧边栏格该,QQ,今日头条,Path(Path运营商最早的侧边栏app该,效果说成是Path效果),所以就研究了下. 然后发现Git Hu ...

  8. 【Qt开发】常用控件--QSpinBox和QDoubleSpinBox

    QSpinBox和QDoubleSpinBox 是UI设计常用的控件. QSpinBox可用于显示和输入整数,并可以在显示框中添加前缀或后缀. QDoubleSpinBox可用于显示和输入小数,并可以 ...

  9. 【Qt开发】常用控件--QLineEdit

    QLineEdit是单行文本编辑控件.比如用户名,密码等输入框可以使用该控件. 所属头文件<QLineEdit> 常用方法 1.void setText(const QString &am ...

随机推荐

  1. VS2012中C++,#include无法打开自己所写的头文件(.h)

    最近刚开始学cocos2d-x,创建项目之后,自己按照<cocos2d-x 3.x 游戏开发>的教程写代码 先写了一个头文件  MyHelloWorldScene.h 然后在  AppDe ...

  2. Spring中@Autowired与@Resource的区别

    1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必 ...

  3. Timestamp转Calendar

    Timestamp scheduleTime = r.getTimestamp("time_recv"); Calendar calendarScheduleTime = Cale ...

  4. HTTP 协议详解(超级经典)-转

    什么是HTTP协议 协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则,超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到客户端 ...

  5. 120. Triangle(中等)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  6. weblogic AND jboss 反序列化漏洞

    C:\Program Files\Java\jboss-4.2.3.GA\server\default\deploy\http-invoker.sar\invoker.war\WEB-INF serv ...

  7. lodop打印收费小票

    //收费系统打印机功能:收费/退费,需要使用到lodop var LODOP;//打印机 $(function () { //初始化 $("body").append('<o ...

  8. JAVA 第二天 内部类

    package com.company; /** * Created by Administrator on 2016/8/23. */ public class Outter {//生成的字节码文件 ...

  9. Laravel-admin 七牛云上传文件到七牛云出现卡顿失败情况

    由于所做项目需要管理后台众多,所以选择了Laravel-admin后台框架进行开发.节省了权限控制以及页面处理等问题的时间 Laravel-admin文档地址 http://laravel-admin ...

  10. ROS新功能包PlotJuggler绘图

    http://www.ros.org/news/2017/01/new-package-plotjuggler.html PlotJuggler,一个基于Qt的应用程序,允许用户加载,搜索和绘图数据. ...