iOS开发知识小集

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

2017-04-03  本文已影响615人  _huawuque
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;
}
上一篇下一篇

猜你喜欢

热点阅读