background image

 iOS 开发:UIImageView 常用操作

UIImageView,顾名思义,是用来放置图片的。使用 Interface Builder 设计界面时,当然可以

直接将控件拖进去并设置相关属性,这就不说了,这里讲的是用代码。

1、创建一个 UIImageView:

创建一个

UIImageView 对象有五种方法:

UIImageView *imageView1 = [[UIImageViewalloc] init];

UIImageView *imageView2 = [[UIImageViewalloc] initWithFrame:(CGRect)];

UIImageView *imageView3 = [[UIImageViewalloc] initWithImage:(UIImage *)];

UIImageView 

*imageView4 

[[UIImageViewalloc] 

initWithImage:(UIImage 

*) 

highlightedImage:(UIImage *)];

UIImageView *imageView5 = [[UIImageViewalloc] 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, CGFloaty, CGFloat width, CGFloat heigth);

那么这个属性跟

frame 有什么区别呢?

我的理解是,

frame 设置其位置和大小,而 bounds 只能设置其大小,其参数中的 x、y 不起

作用即便是之前没有设定

frame 属性,控件最终的位置也不是 bounds 所设定的参数。bounds

实现的是将

UIImageView 控件以原来的中心为中心进行缩放。例如有如下代码:

                   找软件资料,就到一览软件文库

http://wk.yl1001.com/rj/