前言:UI控件整理之UIImageView
一菇篡、UIImageView 效果圖基本代碼實(shí)現(xiàn)
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、設(shè)置本地圖片(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、設(shè)置網(wǎng)絡(luò)圖片
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、設(shè)置圓角
imgView.layer.masksToBounds=YES;
imgView.layer.cornerRadius=10;
4奸汇、設(shè)置邊框顏色和大小
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;
// 設(shè)定所有的圖片在多少秒內(nèi)播放完畢
imgView.animationDuration = [imagesArray count];
// 不重復(fù)播放多少遍擂找,0表示無數(shù)遍
imgView.animationRepeatCount = 0;
// 開始播放
[imgView startAnimating];
2、相對(duì)位置關(guān)系
//????UIViewContentModeScaleToFill
//????UIViewContentModeScaleAspectFit
//????UIViewContentModeScaleAspectFill
//????UIViewContentModeRedraw
//????UIViewContentModeCenter
//????UIViewContentModeTop
//????UIViewContentModeBottom
//????UIViewContentModeLeft
//????UIViewContentModeRight
//????UIViewContentModeTopLeft
//????UIViewContentModeTopRight
//????UIViewContentModeBottomLeft
//????UIViewContentModeBottomRight