一、设置本地图片
UIImageView * iconImg=[[UIImageView alloc]initWithFrame:CGRectMake((kDeviceWidth-90)/2, 10, 90, 90)];
[iconImg setImage:[UIImage imageNamed:@"newIcon"]];
iconImg.contentMode=UIViewContentModeScaleAspectFit;//设置图片填充模式,保持纵横比
UIViewContentModeScaleToFill 拉伸自适应填满整个视图
UIViewContentModeScaleAspectFill 原始大小显示
UIViewContentModeRedraw 尺寸改变时重绘
[self.view addSubview:iconImg];
二、设置网络图片
UIImageView * leftImg=[[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 60, 50)];
leftImg.backgroundColor=[UIColor clearColor];
leftImg.tag=20;
[cell.contentView addSubview:leftImg];
在方法之外调用 UIImageView * leftImg=(UIImageView *)[cell viewWithTag:20];
NewsModel * model=_dataSource[indexPath.row];
[leftImg setImageFromURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",AppImg,model.wximagebig]] placeHolderImage:[UIImage imageNamed:@"newIcon"]];
在UI线程中同步加载网络图片
UIImageView *headview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
NSURL *photourl = [NSURL URLWithString:@"http://www.exampleforphoto.com/pabb/test32.png"];
//url请求实在UI主线程中进行的
UIImage *images = [UIImage imageWithData:[NSData dataWithContentsOfURL:photourl]];//通过网络url获取uiimage
headview.image = images;
三、imageview设置点击事件
imageview1.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress1:)];
[imageview1 addGestureRecognizer:singleTap1];