[UIImage resizableImageWithCapInsets:] 函数说明

来源:互联网 发布:乔任梁网络暴力议论文 编辑:程序博客网 时间:2024/06/15 10:16
关于这个函数,在apple的文档说明中是这样子的
Declare
 - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
Description
Creates and returns a new image object with the specified cap insets.
You use this method to add cap insets to an image or to change the existing cap insets of an image. In both cases, you get back a new image and the original image remains untouched.
During scaling or resizing of the image, areas covered by a cap are not scaled or resized. Instead, the pixel area not covered by the cap in each direction is tiled, left-to-right and top-to-bottom, to resize the image. This technique is often used to create variable-width buttons, which retain the same rounded corners but whose center region grows or shrinks as needed. For best performance, use a tiled area that is a 1x1 pixel area in size.

这个函数,我们所知道的是用它来控制图片拉伸的范围,如
   这坨东西
就是由 这个东西拉伸成功的

对此我们要知道两点
1、当一个UIimage 设置了cap inset后,所设置的capInset是不会拉伸的,但cap Inset划分的范围不是水平与竖直两条线交集的那个小矩形除外的区域,他是按照水平和垂直两个维度来划分的……
简单来说,当一个图片即将拉伸时,首先它会从水平拉伸,除了左右两边的cap inset划分的范围外,拉伸内部的拉伸区域;然后再从垂直的维度进行拉伸,相同的只拉伸capInset之外的部分;
2、拉伸的方式不是通过像素的均匀复制,而是通过镜像复制的方式;
均匀复制,就是如果一个只有3个像素的图 123 ,均匀复制拉伸3倍的话就会变成111222333;而镜像拉伸的话,就会变成123123123;因此要特别注意,而通过这个原理,我们也可以实现一些小效果,比如能把这个东西


通过拉伸,变成这栋东西



以下代码就能搞定~
line = [line resizableImageWithCapInsets:UIEdgeInsetsMake(line.size.height,0,0,0)];




原创粉丝点击