stretchableImageWithLeftCapWidth 的使用

来源:互联网 发布:成都网站制作龙兵网络 编辑:程序博客网 时间:2024/06/08 11:52

UIImage stretchableImageWithLeftCapWidth的使用一直比较迷惑,不明白,并且在开始的使用中总是失败,结合着网上的资料,经过多次实验,终于明白他的用处了, leftCapWidth ,是左侧需要保留的像素数,topCapHeight是顶部需要保留的像素数,然后中间的1像素用于中间的平铺,达到最后所需要的尺寸。效果相当于只能保持一边固定,拉伸另一边。 并且以前使用image后,没有设置到指定的大小,所以没有看到效果。

示例代码:

    UIImage* image = [UIImage imageNamed:@"listbg2"];    image = [image stretchableImageWithLeftCapWidth:floorf(image.size.width/2) topCapHeight:60];       UIImageView* backgrdView = [[UIImageView alloc] initWithFrame:rect];    backgrdView.center = CGPointMake(160.0, 80.0);    backgrdView.contentMode = UIViewContentModeScaleToFill;    backgrdView.image = image;    rect = backgrdView.frame;        [cell.contentView addSubview:backgrdView];

上面,1. backgrdView的起始坐标是相当于父视图,坐标切换容易出错,需要注意坐标的范围,是相对于哪个视图的坐标值。

2. 将一个图片视图作为背景图,并且不同于cell大小,直接设置 backgrounView,backgroundColor,都是有问题的,不如直接添加到contentView  中

只是自己总结,不一定准确,有待后续确认修正