UIImageView总结

UIImageView

  • UIKit框架提供了非常多的UI控件,但并不是每一个都很常用,有些控件可能1年内都用不上,有些控件天天用,比如UIButton、UILabel、UIImageView、UITableView等等
  • UIImageView极其常用,功能比较专一:显示图片
  • 能显示图片,不能直接通过addTarget...方法监听点击

1.常见属性

  • @property(nonatomic,copy) NSArray *animationImages;
    • 显示的动画图片
  • @property(nonatomic,retain) UIImage *image;
    • 显示的图片
  • @property(nonatomic) NSTimeInterval animationDuration;
    • 动画图片的持续时间
  • @property(nonatomic) NSInteger animationRepeatCount;
    • 动画的播放次数(默认是0,代表无限播放)

2.常见方法

  • (void)startAnimating;
    • 开始动画
  • (void)stopAnimating;
    • 停止动画
  • (BOOL)isAnimating;

    • 是否正在执行动画
  • 一个UIImage对象代表一张图片,一般通过imageNamed:方法就可以通过文件名加载项目中的图片

UIImage *image = [UIImage imageNamed:@"Tom"];

3. contentMode属性

  • 带有scale单词的: 图片有可能会拉伸

    • UIViewContentModeScaleToFill(默认)
      • 将图片拉伸至填充整个imageView
      • 图片显示的尺寸跟imageView的尺寸是一样的
    • 带有aspect单词的:图片会保持原来的宽高比
      • UIViewContentModeScaleAspectFit
        • 保持图片的宽高比,保证刚好能看到整个图片
      • UIViewContentModeScaleAspectFill
        • 拉伸到图片宽度或者高度跟imageView一样,并且居中显示
  • 没有带scale单词的: 图片绝对不会被拉伸

    • UIViewContentModeCenter(居中)
    • UIViewContentModeTop(居中靠下)
    • UIViewContentModeBottom(居中靠下)
    • UIViewContentModeLeft(居中靠左
    • UIViewContentModeRight(居中靠右)
    • UIViewContentModeTopLeft(显示在左上角)
    • UIViewContentModeTopRight(显示在右上角)
    • UIViewContentModeBottomLeft(显示在左下角)
    • UIViewContentModeBottomRight(显示在右下角)
    • 图片的属性clipsToBounds = YES(裁剪超出imageView边框的部分)

4. 注意点

  • 用initWithImage默认坐标是(0,0),并且是图片的大小
  • 不能直接修改OC对象的"结构体属性"的成员
    • 取出结构体
    • 赋值

5. 修改frame的3种方式

  • 直接用CGRectMake函数
    • CGPointZero == CGPointMake(0,0);
  • 用临时结构体变量
  • 使用大括号{}
    • 要强制转换类型

6. 帧动画

  • 抽取重复代码
    • 将相同的代码放入一个新的方法
    • 不同的东西变成方法的参数

7. 音频播放

  • 导入
    • URL就算是本地的(直接拖进来[如果含有中文会出错],不能省略file://
    • 必须是全路径
NSURL *url = [NSURL URLWithString:(全路径/本地的(直接拖进来[如果含有中文会出错]))];
AVPlayer *player = [AVPlayer playerWithURL:url];
//不播放,因为player是局部变量,需要定义一个强指针
  • 将音频资料添加到Supporting Files中
    • 全路径:Finder-->前往-->个人-->资源库(隐藏文件夹)-->developer-->CoreSimulator-->Devices-->设备-->data-->Containers-->Bundle-->Application-->应用-->应用名.app
NSURL *url = [NSURL URLWithString:@"file://名称.扩展名"];
//
AVPlayer *player = [AVPlayer playerWithURL:url];
//不播放,因为player是局部变量,需要定义一个强指针
8. 缓存
  • 用imageWithContentsOfFile+全路径
    • 放到images.xcassets就会有缓存,会压缩到一个文件中
    • 将大图片放入到Supporting Files中,并且添加时选中create groups
    • 如果选create folder references则会生成一个文件夹来保存添加的文件

9. 图片的加载方式

  • 有缓存
UIIMage *Image = [UIImage imageNamed:@"图片名"];
- 使用场合: 图片较小,使用频率高
- 放到images.xcassets就会有缓存,会压缩到一个文件中
  • 无缓存
NSString *file = [[NSBundle mainBundle] pathForResource:@"图片名"];
UIImage *image = [UIImage imageWithContentOfFile:@"图片文件的全路径"];
- 使用场合: 图片较大,使用频率较小
- 不能放入images.xcassets中
  • 放入images.xcassets中的图片,只能通过图片名加载图片

10. 延迟做一些事情

[self performSelector:@selector(stand:) withObject:@"123" afterDelay:10];
//10秒后自动调用self的stand: 方法,并且传递@"123"参数

11. 简单播放

//创建一个音频文件
NSURL *url = [NSBundle mainBundle] URLForResource:@"音频文件名" withExtension:@"音频文件的扩展名"];
//创建播放器
AVPlayer *player = [AVPlayer playerWithURL:url];
//播放
[self.player play];
  • 从资源库中加载资料用[NSBundle mainBundle]
原文地址:https://www.cnblogs.com/HMJ-29/p/4758638.html