Apologize

图片的拉伸。如原图为圆角,

若直接作为button的背景图片,那么效果会是这样。

CGFloat btnX = 80;
CGFloat btnY = 150;
CGFloat btnW = 100;
CGFloat btnH = 20;
    
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
UIImage *imageNormal = [UIImage imageNamed:@"audio_button_nomal"];
[btn setBackgroundImage:imageNormal forState:UIControlStateNormal];

此时可以用
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:
该函数是用来创建一个内容可拉伸,而边角不拉伸的图片,需要两个参数,一个是左边不拉伸区域的宽度,另一个是上面不拉伸的高度。
可拉伸的范围是距离leftCapWidth后的1竖排像素,距离topCapHeight后的1横排像素。
如指定参数为10,5。那么表示图片左边10个像素,上边5个像素,不会被拉伸。x坐标为11的一个像素会被横向复制,y坐标为6的一个像素会被纵向复制
对于该图片,可以用

imageNormal = [imageNormal stretchableImageWithLeftCapWidth:floorf(imageNormal.size.width/2) topCapHeight:floorf(imageNormal.size.height/2)];
//floorf(x)表示取不大于x的最大整数
//代码含义为水平方向将图片中间的1像素用来拉伸,垂直方向同理。

效果为

 

分类:

技术点:

相关文章:

  • 2021-09-29
  • 2022-12-23
  • 2022-12-23
  • 2022-02-12
  • 2021-12-29
  • 2022-01-24
  • 2022-12-23
猜你喜欢
  • 2021-11-26
  • 2022-12-23
  • 2022-01-13
  • 2022-01-07
  • 2021-06-04
  • 2021-10-12
  • 2021-07-23
相关资源
相似解决方案