UIImage 的 capInsets 和 resizingMode

08 Jul 2017 by 晓晨


决定一个图片如何变大的两个属性:

注意:阅读 UIImage 的文档时,除非明确指明在说 resizingMode,否则文中提到的 tile 和 stretch 都只是指把图片弄大,并不是指相应的 resizingMode。详情见下段,

文档中出现的 “the entire image is tiled”、”the entire image is subject to stretching”、”when an image is stretched” 等并不是在说 resizingMode,而只是想说这个图片被弄大了。另外,resizingMode 文档下的 “the region will be stretched instead” 也不是在说 resizingMode,强调的是 stretch 这个动作,这句是说,虽然 resizingMode.tile,但这里的动作还是 stretch,因为在这里,二者效果一样,效率不同。

UIImage 本身就是不可变的,要想修改这些属性,只能通过特定函数返回一个新的 UIImage。

resizingMode

没什么说的,.tile 或 .stretch

比如 1 平方米的图片要放在 4 平方米的地方,

前者,放四张图片在这个地方,

后者,把图片的长宽都拉长。

capInsets

默认情况下,图片整体变大,但是有些情况下希望图片的边缘部分尽量不变,如图,

准确地说,是四个角不变,四个边缘横变或竖变,中间区域横竖两个方向都变。

capInsets: UIEdgeInsets 就是用来决定这个边缘部分的,注意这四个边都是可以为零的。

修改 capInsets 的两个方法:

区别就是是否指定 resizingMode (.tile and .stretch)

若不指定,var resizingMode: UIImageResizingMode { get } 的值不变,capInsets 的文档中所说的 “the entire image is tiled” 只是强调动作针对整个 image,并不是说 resizingMode 是 .tile。为此,做了个试验

capInsets、resizingMode 的效率影响

TL;DR


UIImage 的文档中两处涉及关于这两个属性的效率,