一般的图片拉伸可以用系统自带的show slicing,但是气泡尖头居中朝上或者朝下没办法使用此方法,可以这样:
- (UIImage *)lg_stretchLeftAndRightWithimg:(UIImage*)img withsize:(CGSize)size
{
CGSize imageSize = img.size;
CGSize bgSize = size;
//1.第一次拉伸右边 保护左边
UIImage *image = [img stretchableImageWithLeftCapWidth:imageSize.width *0.8 topCapHeight:imageSize.height * 0.5];
//第一次拉伸的距离之后图片总宽度
CGFloat tempWidth = (bgSize.width)/2 + imageSize.width/2;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(tempWidth, imageSize.height), NO, [UIScreen mainScreen].scale);
[image drawInRect:CGRectMake(0, 0, tempWidth, bgSize.height)];
//拿到拉伸过的图片
UIImage *firstStrechImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//2.第二次拉伸左边 保护右边
UIImage *secondStrechImage = [firstStrechImage stretchableImageWithLeftCapWidth:firstStrechImage.size.width *0.2 topCapHeight:firstStrechImage.size.height*0.5];
return secondStrechImage;
}
注意:拉伸图片尺寸不能是奇数,奇数情况下会导致拉伸后的图片有一条白线。
网友评论