[Geekband]iOS应用开发实战 - UIImage 笔记

iOS设备的多样性

曾经很简单,现在越来越复杂

[Geekband]iOS应用开发实战 - UIImage 笔记_第1张图片
iOS设备的多样性

以上还不包含iPad、Watch、Apple TV等等

更详细的清单可见:https://en.wikipedia.org/wiki/List_of_iOS_devices

界面适配会在后面讲

Assert Catalog

管理项目里的图片

[Geekband]iOS应用开发实战 - UIImage 笔记_第2张图片
Assert Catalog

可以可视化的管理各尺寸的图片

1pt,1x图片尺寸为1px,2x图片尺寸为2px,3x图片尺寸为3px

图片切片

切片多用于方框,可以使图片在拉伸时四角保持一定的比例

在Attribute Inspector -> slicing

或点击Show Slicing

[Geekband]iOS应用开发实战 - UIImage 笔记_第3张图片
图片切片

矢量图

  • PDF格式的矢量图创建
    • Illustrator 或 Inkscape 到处 PDF
    • Keynote 也可以
  • @1x尺寸
  • 在Assert Catalog里设置
    • Scale Factors: Single Vector
    • 然后将矢量图拖进Universal -> All

Vector With Override : 优先使用1x、2x、3x版本,若未提供,则使用矢量图

UIImageView

Mode:图片拉伸方式

Alpha:半透明度,比较影响性能

  • 用UIImageView 显示图片
    • .image属性
    • 图片从哪里来的?
      • 在IB里选图片意味着
        • 图片打包在mainBundle里
        • 系统自动缓存,且不会释放

UIImage 对象

加载App bundle里的图片

//会自动缓存到内存中,且不会释放
image = [UIImage imageNamed:@"mypic"];

//会自动缓存到内存中,且不会释放,bundle及其他参数可以自己指定
image = [UIImage imageNamed:@"mypic" inBundle:nil conpatibleWithTraitCollection:nil];

//不会自动缓存到内存中
NSURL* url = [[NSBundle mainBundle] URLForResource:@"mypic" withExtension:@"png"];
UIImage* image = [UIImage imageWithContentsOfFile:url.path];

加载文件里的图片

//不会自动缓存到内存中
UIImage* image = [UIImage imageWithContentsOfFile:path];

//不会自动缓存到内存中
UIImage* image = [UIImage imageWithData:data];

使用animatedImageNamed:方法,系统会自动识别类似image01.xx、image02.xx。。。这样的命名,然后将他们显示为动图:

UIImage *image = [UIImage animatedImageNamed:@"image" duration: 1.0];

但是要注意内存的占用

你可能感兴趣的:([Geekband]iOS应用开发实战 - UIImage 笔记)