iOS 图片拉伸:两边拉伸,中间保持不变

iOS 图片拉伸:两边拉伸,中间保持不变_第1张图片
WX20170403-114055.png

(网络图片,侵删)
创建UIImage分类中的方法
- (UIImage *)dc_stretchLeftAndRightWithContainerSize:(CGSize)imageViewSize
{
CGSize imageSize = self.size;
CGSize bgSize = CGSizeMake(floorf(imageViewSize.width), floorf(imageViewSize.height)); //imageView的宽高取整,否则会出现横竖两条缝

UIImage *image = [self stretchableImageWithLeftCapWidth:imageSize.width *0.8 topCapHeight:imageSize.height * 0.5];
CGFloat tempWidth = (bgSize.width)/2 + (imageSize.width)/2;

UIGraphicsBeginImageContextWithOptions(CGSizeMake(tempWidth, bgSize.height), NO, [UIScreen mainScreen].scale);

[image drawInRect:CGRectMake(0, 0, tempWidth, bgSize.height)];

UIImage *firstStrechImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImage *secondStrechImage = [firstStrechImage stretchableImageWithLeftCapWidth:imageSize.width *0.2 topCapHeight:imageSize.height*0.5];

return secondStrechImage;
}

你可能感兴趣的:(iOS 图片拉伸:两边拉伸,中间保持不变)