UIImageView详解

来源:互联网 发布:流前列腺液的好处知乎 编辑:程序博客网 时间:2024/05/22 23:12

声明:iOS开发小白程序员一枚,正在新手上路级别,行文中如有错误,还望各位批评指正,谢谢^^


UIImageView创建的几种方式

- (void)createUIImageView{    //创建UIImageView    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];        //设置普通状态下的图片    imageView.image = [UIImage imageNamed:@"01"];        //设置高亮状态下的图片    imageView.highlightedImage = [UIImage imageNamed:@"02"];        //将视图添加到视图上    [self.view addSubview:imageView];}
- (void)createUIImageView{    //创建UIImageView    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"03"] highlightedImage:[UIImage imageNamed:@"04"]];        //设置位置    imageView.frame = CGRectMake(0, 120, 320, 100);        //将视图添加到视图上    [self.view addSubview:imageView];}
- (void)createUIImageView{    //创建UIImageView    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 240, 320, 100)];        //沙盒中图片文件的路径    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"05@2x" ofType:@"png"];        //设置显示沙盒中图片    imageView.image = [UIImage imageWithContentsOfFile:filePath];        //将视图添加到视图上    [self.view addSubview:imageView];}
- (void)createUIImageView{    //创建UIImageView    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 240, 320, 100)];        //沙盒中图片文件的路径    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"06@2x" ofType:@"png"];        //将图片转为二进制数据形式    NSData *data = [NSData dataWithContentsOfFile:filePath];        //设置显示二进制数据形式的图片    imageView.image = [UIImage imageWithData:data];        //将视图添加到视图上    [self.view addSubview:imageView];}

备注说明:

1.使用imageNamed方法加载图片,会将图片直接Cache到内存中来,对内存的消耗比较大,但同一个图片只会加载到内存中一次,这对图片需要重复利用来说又是一大优势

2.利用NSData方法加载图片,图片会被系统以数据方式加载到程序中,当你不需要重用该图片,或者你需要将图片以数据方式存储到数据库,又或者你要通过网络下载一个很大的图片时,尽量使用imageWithData的方式加载图片。


UIImageView几个属性的设置

- (void)setProperty{    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"01"] highlightedImage:[UIImage imageNamed:@"02"]];    imageView.frame = CGRectMake(0, 0, 320, 100);    [self.view addSubview:imageView];        imageView.highlighted = YES; //是否高亮,默认NO        imageView.userInteractionEnabled = YES; //是否开启用户交互(默认UILabel和UIImageview都是NO,则加在该控件上边的所有其他控件也都转为用户触摸关闭模式)        imageView.contentMode = UIViewContentModeCenter; //设置图片的显示方式为保持比例不缩放居中显示,默认图片会被拉伸/压缩成与imageView相同的大小    imageView.contentMode = UIViewContentModeScaleAspectFill; //设置图片的显示方式为保持比例缩放居中显示,默认图片会被拉伸/压缩成与imageView相同的大小    imageView.clipsToBounds  = YES; //设置图片的显示方式后,将多出来的部分截掉        [imageView sizeToFit]; //将imageView尺寸调整为与内容图片相同        imageView.transform = CGAffineTransformMakeScale(0.6, 0.6); //缩放,参数为宽高缩放比例    imageView.transform = CGAffineTransformMakeRotation(M_PI*90/180.0); //顺时针旋转,参数为弧度,可以宏定义    imageView.transform = CGAffineTransformMakeTranslation(160, 100); //向x和y方向移动多少,不是移动到多少}

UIImageView图片轮播动画,能用代码解决的问题尽量少用图片,不建议使用
- (void)imageAnimation{    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];    [self.view addSubview:imageView];        imageView.animationImages = @[[UIImage imageNamed:@"01"],[UIImage imageNamed:@"02"],[UIImage imageNamed:@"03"],[UIImage imageNamed:@"04"],[UIImage imageNamed:@"05"],[UIImage imageNamed:@"06"]]; //动画素材图片数组    imageView.animationDuration = 3; //设定所有的图片在多少秒内播放完毕    imageView.animationRepeatCount = 3; //重复播放多少遍,0表示无数遍    [imageView startAnimating]; //开始播放}
备注说明:图片轮播只在非高亮状态下可以使用


demo下载地址 点击这里下载demo

0 0
原创粉丝点击