UIImageView

  • UIImageView:UIView UIView的所有属性和方法UIImageView都拥有
  • UIImageView专门用来显示图片的控件

目录

  • UIImage图片类
  • 设置内容模式(UIImageView上图片的比例)
  • 设置帧动画

1.创建一个UIImageView对象

UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];

2.设置背景颜色

imageView.backgroundColor = [UIColor redColor];

UIImage图片类

  • 根据图片名创建图片(前提是图片已经添加到工程中),拖到工程中的图片,实质是当前这个应用程序的安装包里面
  • 参数:图片的名字(如果图片是png图片,那么图片名的后缀可以省略;如果是其他格式,后缀名必须加上)
+ (UIImage *)imageNamed:(NSString *)name;
    UIImage * image = [UIImage imageNamed:@"back2.jpg"];
    NSLog(@"%p", image);
根据文件路径来创建图片
  • 参数:图片文件的路径
+ (UIImage *)imageWithContentsOfFile:(NSString *)path;
拿到包中图片的路径(可以拿到当前工程中任意文件的路径)
  • 参数1:文件的名字
  • 参数2:文件的后缀
NSString * path = [[NSBundle mainBundle] pathForResource:@"player_down_1" ofType:@"jpg"];
UIImage * image2 = [UIImage imageWithContentsOfFile:path];
获取图片的大小
CGSize imagesize = image.size;
NSLog(@"%@", NSStringFromCGSize(imagesize));
设置图片(核心属性)
[imageView setImage:image];

设置内容模式

[imageView setContentMode:UIViewContentModeScaleToFill];
  • 将图片缩放(按imageView缩放),完整的显示在imageView上(默认)
UIViewContentModeScaleToFill
  • 将图片按比例缩放(图片本身的比例),完整的显示在imageView上
UIViewContentModeScaleAspectFit
  • 图片按比例缩放,imageView也按图片的比例缩放了 (这个一般不考虑)
UIViewContentModeScaleAspectFill
  • 效果和UIViewContentModeScaleToFill一样
UIViewContentModeRedraw
以下模式图片不缩放,还影响imageView的frame(一般不会用到)
UIViewContentModeCenter,
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,

设置帧动画

  • 1.创建UIImageView对象
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 80, 80)];
  • 2.显示图片(显示静态图片)--帧动画结束后会显示静态图片
imageView.image = [UIImage imageNamed:@"DOVE 1"];
  • 3.添加到界面上
[self.window addSubview:imageView];

显示帧动画

  • 创建图片数组
    NSMutableArray * images = [[NSMutableArray alloc] init];
    
    for (int i = 1; i < 19; i++) {
      //创建图片
      UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"DOVE %d", i]];
        
      //将图片添加到图片数组中
      [images addObject:image];
        
    }
  • 4.设置动画图片数组(数组元素必须是UIImage对象)
[imageView setAnimationImages:images];
  • 5.设置播放完一组动画所需要的时间(单位:秒)
[imageView setAnimationDuration:1];
  • 6.设置循环次数(默认是1)
//播放次数设成0,相当于无限循环
[imageView setAnimationRepeatCount:5];
  • 7.开始播放动画
[imageView startAnimating];
  • 8.停止动画
  [imageView stopAnimating];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,599评论 6 30
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,158评论 5 13
  • 1.初始化 UIImageView *imageView=[[UIImageView alloc] initWit...
    阿尔法代码狗阅读 727评论 0 0
  • UIImageView 文档,https://developer.apple.com/reference/uiki...
    阿斯兰iOS阅读 2,245评论 0 0
  • //UIButton和UIImageView常用属性和常用方法总结 //MARK: - UIButton常用属性和...
    专注_刻意练习阅读 756评论 0 0