UIImageView是在界面上显示图片的一个控件,在UIImageView中显示图片的话应该首先把图片加载到UIImage中,然后通过其他方式使用该UIImage。

创建UIImageView有两种方法:

一种是用UIImage来加载:
UIImage *image = [UIImageimageNamed:@"picture"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

另一种是通过initWithFrame:来加载,然后手工修改UIImageView的属性:

UIImageView *imageView = [[UIImageView alloc] initWithImage:@"picture"];

    //这里我用了第一种方法
UIImage *image = [UIImage imageNamed:@"u=2255024074,2191209375&fm=21&gp=0"];
//初始化
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
//背景颜色
imageView.backgroundColor = [UIColor blueColor];
//圆角
imageView.layer.cornerRadius = ;
imageView.layer.masksToBounds = YES;
//是否交互
imageView.userInteractionEnabled = YES;
//宽与高分别采用原图片的宽与高
float width = imageView.frame.size.width;
float height = imageView.frame.size.height;
//设置imageView的位置与尺寸
CGRect frame = imageView.frame;
frame.origin.x = ;
frame.origin.y = ;
frame.size.width = width;
frame.size.height = height;
[imageView setFrame:frame];

UIImageView的几种常用属性:

(1)设置圆角

imageView.layer.masksToBounds = YES;

imageView.layer.cornerRadius = 10;

(2)设置边框颜色和大小

imageView.layer.borderColor = [UIColor orangeColor];

imageView.layer.borderWidth = 2;

(3)contentMode属性:当图片小于imageView的大小处理图片显示

这个属性是用来设置图片的显示方式,如居中、居右,是否缩放等,有以下几个常量可供设定:

UIViewContentModeScaleAspectFit:这个图片都会在view里面显示,并且比例不变,这就是说,如果图片和view的比例不一样就会有留白。

UIViewContentModeScaleAspectFill: 这是整个view会被图片填满,图片比例不变 ,这样图片显示就会大于view。

UIViewContentModeCenter ;UIViewContentModeTop;UIViewContentModeBottom;UIViewContentModeLeft;UIViewContentModeRight;

UIViewContentModeTopLeft;UIViewContentModeTopRight;UIViewContentModeBottomLeft;UIViewContentModeBottomRight(这几种就不一一解

释了,都是保持图片比例不变)。

(4)为图片添加单击事件:一定要先将userInteractionEnabled置为YES,这样才能响应单击事件

imageView.userInteractionEnabled = YES;

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ImageViewAction:)];

[imageView addGestureRecognizer:tap];

- (void)ImageViewAction:(UITapGestureRecognizer *)gesture{

imageView.backgroundColor = [UIColor yellowColor];

}

(5)播放一系列图片

UIImage *image1 = [UIImage imageNamed:@"p1"];

UIImage *image2 = [UIImage imageNamed:@"p2"];

UIImage *image3 = [UIImage imageNamed:@"p3"];

NSArray *imagesArray = @[image1,image2,image3];

imageView.animationImages = imagesArray;

//设定所有的图片在多少秒内播放完毕

imageView.animationDuration = [imagesArray count];

//不重复播放多少遍,0表示无数遍

imageView.animationRepeatCount = 0;

// 开始播放

[imageView startAnimating];

最新文章

  1. slf4j
  2. android:布局、绘制、内存泄露、响应速度、listview和bitmap、线程优化以及一些优化的建议!
  3. 在JS中获取文件点之后的后缀字符
  4. jquery $.each的用法
  5. Oracle_SQL函数-分组函数
  6. 图解c/c++多级指针与“多维”数组
  7. Docker简明教程
  8. 基于jQuery的对象切换插件:soChange 1.5 (点击下载)
  9. MySql 分页
  10. 使用JDBC对数据库进行查询的前期准备工作,以及简单的JDBC访问MySQL数据库(Mac)
  11. python判断类型
  12. jquery mouseout事件错误(bug)
  13. shell find and rm
  14. 多线程下OpenCV操作的问题
  15. Nicholas C. Zakas如何面试前端工程师
  16. 实战Tomcat配置SSL,使用openssl制作证书
  17. JAVA并发编程学习笔记------基础构建模块
  18. Oracle中的AWR,全称为Automatic Workload Repository
  19. 项目ITP(一) 二维码
  20. The Monocycle(bfs)

热门文章

  1. UpdatePanel的简单用法(转)
  2. [转 ]-- Java线程池使用说明
  3. 个人作业Week 2 ----------代码的规范和代码复审
  4. Scrum Meeting---One(2015-10-20)
  5. iOS - UIWebView
  6. 06 SQL执行计划
  7. golang json
  8. [js]事件综合 整理
  9. JAVA调用C语言写的SO文件
  10. ORACLE CentOS5.6安装