UI之UIImageView--属性及用法

  1 // 新建UIImageView 5种方法
  2     // 设置位置及大小、添加图片、添加到视图
  3     UIImageView* image1 = [[UIImageView alloc]init];
  4     image1.frame = CGRectMake(10, 40, 300, 300);
  5     image1.image = [UIImage imageNamed:@"410825"];
  6     [self.view addSubview:image1];
  7     
  8     UIImageView* image2 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 40, 300, 300)];
  9     [image2 setImage:[UIImage imageNamed:@"410825"]];
 10     [self.view addSubview:image2];
 11     
 12     UIImageView* image3 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"410825"]];
 13     image3.frame = CGRectMake(10, 40, 300, 300);
 14     [self.view addSubview:image3];
 15     
 16     // 这种新建,当ImageView的highlighted的属性是YES时,显示的就是参数highlightedImage,一般情况下显示的就是第一个参数UIImage
 17     UIImageView* image4 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"410825"] highlightedImage:[UIImage imageNamed:@"410825"]];
 18     image4.frame = CGRectMake(10, 40, 300, 300);
 19     [self.view addSubview:image4];
 20     
 21    //  UIImageView* image5 = [[UIImageView alloc]initWithCoder:<#(NSCoder *)#> ];
 22     
 23     
 24     // frame与bounds属性
 25     image4.frame = CGRectMake(10, 40, 300, 300);
 26     // 注意bounds属性的坐标是相对与父视图的左上角来讲的
 27     image4.bounds = CGRectMake(10, 10, 300, 300);
 28     
 29     // 更改UIImageView的位置除了修改frame属性还可以修改center属性
 30     image4.center = CGPointMake(16, 240);
 31     
 32     // transform 形变属性 向x轴y同时轴移动10
 33     image4.transform = CGAffineTransformMakeTranslation(10, 10);
 34     
 35     // 旋转 (以ImageView的中心点也就是center属性表示的位置顺时针旋转90度)
 36     image4.transform = CGAffineTransformRotate(image4.transform, M_PI_2);
 37     
 38     // 缩放图片 (缩放到原来的0.6倍)
 39     image4.transform = CGAffineTransformScale(image4.transform, 0.6, 0.6);
 40     
 41     // 设置图片的显示方式,如居中、居左,是否缩放等,有一下几个常量可供设定:
 42     image4.contentMode = UIViewContentModeBottom;
 43     typedef  enum{
 44         UIViewContentModeBottom, // 底部
 45         UIViewContentModeBottomLeft, // 左下
 46         UIViewContentModeBottomRight, // 右下
 47         UIViewContentModeCenter,  // 中心
 48         UIViewContentModeLeft, // 左边
 49         UIViewContentModeRedraw,  //
 50         UIViewContentModeRight,  // 右边
 51         UIViewContentModeScaleAspectFill,  // 等比
 52         UIViewContentModeScaleAspectFit,  // 不失真(适合)
 53         UIViewContentModeScaleToFill,  // 等比
 54         UIViewContentModeTop, // 上面
 55         UIViewContentModeTopLeft, // 左上
 56         UIViewContentModeTopRight, // 右上
 57     }UIViewContentMode;
 58     /*
 59      注意以上几个常量,凡是没有带Scale的,当图片尺寸超过 ImageView尺寸时,只有部分显示在ImageView中。UIViewContentModeScaleToFill属性会导致图片变形。UIViewContentModeScaleAspectFit会保证图片比例不变,而且全部显示在ImageView中,这意味着ImageView会有部分空白。UIViewContentModeScaleAspectFill也会证图片比例不变,但是是填充整个ImageView的,可能只有部分图片显示出来。
 60      */
 61     
 62     // 显示或隐藏图片
 63     image4.hidden = NO;
 64     
 65     // 设置透明度
 66     image4.alpha = 0.5;
 67     
 68     // 设置高亮时显示的图片
 69     image4.highlightedImage = [UIImage imageNamed:@"410825"];
 70     
 71     // 正常显示图片、
 72     image4.image = [UIImage imageNamed:@"410825"];
 73     
 74     // 设置高亮
 75     image4.highlighted = YES;
 76     
 77     // 将图片尺寸调整为与内容如片相同
 78     // [image4 seizToFit];
 79     
 80     
 81     // 播放一系列图片
 82     NSArray* imagesArray = [NSArray arrayWithObjects:@"图片数组",nil];
 83     image4.animationImages = imagesArray;
 84     // 设定所有的图片在多少秒内播放完毕
 85     image4.animationDuration = [imagesArray count];
 86     // 重复播放次数,0表示无数遍
 87     image4.animationRepeatCount = 0;
 88     // 开始播放
 89     [image4 startAnimating];
 90     
 91     
 92     // 图片不多时,为了效率用这种方法加载图片(图片会放到缓存中,图片多时会造成内存问题)
 93     image1.image = [UIImage imageNamed:@"410825"];
 94     
 95     // 图片多时,用这种方法加载图片(加载本地图片,防止内存问题)
 96     image1.image = [UIImage imageWithContentsOfFile:@"本地图片路径"];
 97     
 98     // 加载网络图片时,用这种方法
 99     image1.image = [UIImage imageWithData:nil]; // date转image // 网络图片路径
100    //  NSDate* date = [UIImagePNGRepresentation(image对象)];  // image转date
101     
102     
103     // 为图片添加点击事件
104     // 是否可以和用户交互(一定要先将userInteractionEnable设置为YES,这样才能响应点击事件)
105     image4.userInteractionEnabled = YES; // 默认是NO(特别)
106     UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageView:)];
107     [image4 addGestureRecognizer:singleTap];
108 
109 
110  NSArray* _imageName =[NSArray arrayWithObjects:@"少司命",@"少司命2",@"背景",@"背景2",@"背景3",@"素材01",@"素材02",@"素材03",@"素材04",@"素材05", nil];
111     // 获取数组里指定名称对象的下标
112     int index = [_imageName indexOfObject:@"素材01"];
113     NSLog(@"index = %d",index);
114     
115      // 获取本地图片
116     NSArray* arrayjpg =[[NSBundle mainBundle]pathForResource:nil ofType:@"jpg"];
117     
118     NSArray* arraypng = [[NSBundle mainBundle]pathForResource:nil ofType:@"png" inDirectory:nil];
119 
120     // nil代表全路径
121     NSArray* array = [[NSBundle mainBundle]pathsForResourcesOfType:@"jpg" inDirectory:nil];
122     
123     // 合并
124     NSArray* a = [arrayjpg arrayByAddingObjectsFromArray:arraypng];
125     
126     // 如果上面合并有一项为空的情况下 用 / 分割
127     NSMutableArray* name = [NSMutableArray array];
128     for (NSString* path in arraypng) {
129         NSArray * cap = [path componentsSeparatedByString:@"/"];
130         [name addObjectsFromArray:cap];
131     }
132 
133 }
134 -(void)tapImageView:(UIImageView*)sender{
135     NSLog(@"UIImageView");
136 }
原文地址:https://www.cnblogs.com/WillingToAsk1946zzh/p/4504463.html