前言:UI控件整理之UIImageView
一、UIImageView 效果图基本代码实现
UIImageView *imgView = [[UIImageView alloc]init];
imgView.frame = CGRectMake(20, 60, [UIScreen mainScreen].bounds.size.width - 40, 220);
imgView.contentMode = UIViewContentModeScaleAspectFit;
imgView.image = [UIImage imageNamed:@"timg"];
[self.view addSubview:imgView];
imgView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapImageView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageAction)];
[imgView addGestureRecognizer:tapImageView];
二、部分常用属性解析
1、设置本地图片(3种方式)
(1)第一种
[imageView setImage:[UIImage imageNamed:@"1.jpeg"]];
(2)第二种
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"timg" ofType:@"jpg"];
UIImage *images = [UIImage imageWithContentsOfFile:filePath];
[imgView setImage:images];
(3)第三种
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"timg" ofType:@"jpg"];
NSData *data=[NSData dataWithContentsOfFile:filePath];
UIImage *image2=[UIImage imageWithData:data];
[imgView setImage:image2];
2、设置网络图片
UIImage *urlImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1510311357723&di=5d10e3dca9da10b33a1cf3f2b7807b0f&imgtype=0&src=http%3A%2F%2Fpic7.photophoto.cn%2F20080529%2F0034034465128235_b.jpg"]]];
imgView.image = urlImage;
3、设置圆角
imgView.layer.masksToBounds=YES;
imgView.layer.cornerRadius=10;
4、设置边框颜色和大小
imgView.layer.borderColor= [UIColororangeColor].CGColor;
imgView.layer.borderWidth=2;
三、属性拓展
1、播放一系列图片
UIImage *image1 = [UIImage imageNamed:@"img1"];
UIImage *image2 = [UIImage imageNamed:@"img1"];
UIImage *image3 = [UIImage imageNamed:@"img1"];
NSArray *imagesArray = @[image1,image2,image3];
imgView.animationImages = imagesArray;
// 设定所有的图片在多少秒内播放完毕
imgView.animationDuration = [imagesArray count];
// 不重复播放多少遍,0表示无数遍
imgView.animationRepeatCount = 0;
// 开始播放
[imgView startAnimating];
2、相对位置关系
// UIViewContentModeScaleToFill
// UIViewContentModeScaleAspectFit
// UIViewContentModeScaleAspectFill
// UIViewContentModeRedraw
// UIViewContentModeCenter
// UIViewContentModeTop
// UIViewContentModeBottom
// UIViewContentModeLeft
// UIViewContentModeRight
// UIViewContentModeTopLeft
// UIViewContentModeTopRight
// UIViewContentModeBottomLeft
// UIViewContentModeBottomRight