博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UIImageview的简单运用
阅读量:4591 次
发布时间:2019-06-09

本文共 2310 字,大约阅读时间需要 7 分钟。

UIImageView,顾名思义,是用来放置图片的。使用Interface Builder设计界面时,当然可以直接将控件拖进去并设置相关属性,这就不说了,这里讲的是用代码。

1、创建一个UIImageView:

创建一个UIImageView对象有五种方法:

UIImageView *imageView1 = [[UIImageView alloc] init];UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:(CGRect)];UIImageView *imageView3 = [[UIImageView alloc] initWithImage:(UIImage *)];UIImageView *imageView4 = [[UIImageView alloc] initWithImage:(UIImage *) highlightedImage:(UIImage *)];UIImageView *imageView5 = [[UIImageView alloc] initWithCoder:(NSCoder *)];

 

 

比较常用的是前边三个。至于第四个,当这个ImageView的highlighted属性是YES时,显示的就是参数highlightedImage,一般情况下显示的是第一个参数UIImage。

2、frame与bounds属性:

上述创建一个UIImageView的方法中,第二个方法是在创建时就设定位置和大小。

当之后想改变位置时,可以重新设定frame属性:

imageView.frame = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat heigth);

 

 

注意到UIImageView还有一个bounds属性

imageView.bounds = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat heigth);

 

 

那么这个属性跟frame有什么区别呢?

我的理解是,frame设置其位置和大小,而bounds只能设置其大小,其参数中的x、y不起作用即便是之前没有设定frame属性,控件最终的位置也不是bounds所设定的参数。bounds实现的是将UIImageView控件以原来的中心为中心进行缩放。例如有如下代码:

imageView.frame = CGRectMake(0, 0, 320, 460);imageView.bounds = CGRectMake(100, 100, 160, 230);

 

 

执行之后,这个imageView的位置和大小是(80, 115, 160, 230)。

3、contentMode属性:

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

UIViewContentModeScaleToFillUIViewContentModeScaleAspectFitUIViewContentModeScaleAspectFillUIViewContentModeRedrawUIViewContentModeCenterUIViewContentModeTopUIViewContentModeBottomUIViewContentModeLeftUIViewContentModeRightUIViewContentModeTopLeftUIViewContentModeTopRightUIViewContentModeBottomLeftUIViewContentModeBottomRight

 

 

注意以上几个常量,凡是没有带Scale的,当图片尺寸超过 ImageView尺寸时,只有部分显示在ImageView中。UIViewContentModeScaleToFill属性会导致图片变形。UIViewContentModeScaleAspectFit会保证图片比例不变,而且全部显示在ImageView中,这意味着ImageView会有部分空白。UIViewContentModeScaleAspectFill也会证图片比例不变,但是是填充整个ImageView的,可能只有部分图片显示出来。

  UIViewContentModeScaleToFill    UIViewContentModeScaleAspectFit  UIViewContentModeScaleAspectFill

4、更改位置

更改一个UIImageView的位置,可以

4.1 直接修改其frame属性

4.2 修改其center属性:

imageView.center = CGPointMake(CGFloat x, CGFloat y);

 

 

center属性指的就是这个ImageView的中间点。

4.3 使用transform属性

imageView.transform = CGAffineTransformMakeTranslation(CGFloat dx, CGFloat dy);

 

 

其中dx与dy表示想要往x或者y方向移动多少,而不是移动到多少。

5、旋转图像

imageView.transform = CGAffineTransformMakeRotation(CGFloat angle);

转载于:https://www.cnblogs.com/anjiubo/p/5267476.html

你可能感兴趣的文章
jquery快速入门三
查看>>
分布式锁 原理及实现方式
查看>>
四则运算三完整版
查看>>
18.3 线程的声明周期
查看>>
fomo6d游戏系统开发 fomo6d游戏
查看>>
div简单布局理解
查看>>
EasyUI Tree判断节点是否是叶
查看>>
Java基础加强总结(一)——注解(Annotation)
查看>>
Windows 2008R2关闭网络发现
查看>>
hibernate tool连接oracle生成pojo和xml文件无法查询表解决办法
查看>>
Jenkins执行selenium报错unknown error: cannot find Chrome binary
查看>>
Content-Type四种常见取值
查看>>
禹庙-杜甫
查看>>
Cache缓存
查看>>
[家里蹲大学数学杂志]第409期与正弦对数有关的一个积分不等式
查看>>
BZOJ 2795: [Poi2012]A Horrible Poem (Hash+思维)
查看>>
HDOJ-1002
查看>>
五笔字根表小记
查看>>
用H5中的Canvas等技术制作海报
查看>>
ASP.NET性能优化之减少请求
查看>>