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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值