上圖中間區域的翅膀不能變形,Android的點九能很好的解決,可是iOS的stretchableImageWithLeftCapWidth無法一次性指定拉伸區域,最後發現https://www.jianshu.com/p/bb3300cf15c4 的解決思路,而後修改了下,/**
拉伸圖片 -- 拉伸四周保持中間大圖不變形
@param size 所須要設置的大小
@param baseImage 原圖
@return 拉伸好的圖片
*/
- (UIImage *)dc_stretchLeftAndRightWithContainerSize:(CGSize)size baseImage:(UIImage *)baseImage
{
// 248 中間圖片的寬 273 中間圖片的高
CGFloat top = (baseImage.size.height - 248)/2;
CGFloat left = (baseImage.size.width - 273)/2;
CGSize imageSize = baseImage.size;
CGSize bgSize = size;
//1.第一次拉伸下面 保護上面
UIImage *image = [baseImage stretchableImageWithLeftCapWidth:left+273 topCapHeight:top+248];
//第一次拉伸的距離以後圖片總寬度
CGFloat tempWidth = (bgSize.width)/2 + imageSize.width/2;
CGFloat tempHeight = (bgSize.height)/2 + imageSize.height/2;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(image.size.width, tempHeight), NO, [UIScreen mainScreen].scale);
[image drawInRect:CGRectMake(0, 0, tempWidth, tempHeight)];
//拿到拉伸過的圖片
UIImage *firstStrechImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//2.第二次拉伸上面 保護下面
UIImage *secondStrechImage = [firstStrechImage stretchableImageWithLeftCapWidth:left topCapHeight:top];
return secondStrechImage;
}blog
能夠有效解決圖片