当前位置: 代码迷 >> 综合 >> - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCap
  详细解决方案

- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCap

热度:99   发布时间:2023-12-11 14:51:48.0

看书看到这个方法,现在有些遗忘,顺便做个小小的笔记。

- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;

 这个函数是UIImage的一个实例函数,它的功能是创建一个内容可拉伸,而边角不拉伸的图片,需要两个参数,第一个是左边不拉伸区域的宽度,第二个参数是上面不拉伸的高度。我自己的理解(认识)是在左边、上面找一个基点,对基点范围以外的内容拉伸。

找了一个张60*55的图做了个实验,代码如下:

定义了一个大小为210*110的UIImageView,要让60*55的这个UIImage能够拉伸填充进来。

UIImageView *m_imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,240,110)];

[m_imageView setImage:[[UIImageimageNamed:@"2.png"]stretchableImageWithLeftCapWidth:30topCapHeight:10]];

[self.view addSubview:m_imageView];

[m_imageView release];

下面的效果图就能很直接的看到:第一个图是通过- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;方法得到的,第二张图是60*55的原始图;第三张图是240*110没处理直接得到的图。


上面这张效果图是我对(原始图)左边坐标30处的的拉伸效果,下面这张是在坐标40处的效果,如下


通过比较说明自己的认识是正确的。