UIImageView如何以九宫格方式拉伸一个图片

来源:互联网 发布:w10怎么卸载软件 编辑:程序博客网 时间:2024/05/16 08:59

                                      UIImageView如何以九宫格方式拉伸一个图片

        首先要创建一个普通的UIImage,然后用UIImage的 stretchbleImageWithLeftCapWidth:  topCapHeight方法,指定左右两边保持不拉伸的范围,和上下两边保持不拉伸的范围,创建出一个新的UIImage.

       然后将这个UIImage设置到需要的UIImageView中。

      最后,设置UIImageView的frame,这样UIImageView就会自己根据frame的大小去保持四个角不拉伸的方式去拉伸图片啦。

UIImage *contentBgImagebubble = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fromSelf?@"bubbleSelf":@"bubble" ofType:@"png"]];            UIImage * newBgImage =[contentBgImagebubble stretchableImageWithLeftCapWidth:21 topCapHeight:14] ;            UIImageView * contentBgImage=[[UIImageView alloc] initWithImage:newBgImage];            contentBgImage.frame=CGRectMake(10,10,200,100);


原创粉丝点击