参考链接
在iOS6.0中,UIImage提供了一个方法处理图片拉伸
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode
方法解释:
参数: resizingMode,指定拉伸模式
UIImageResizingModeStretch:拉伸模式,通过拉伸UIEdgeInsets指定的矩形区域来填充图片
UIImageResizingModeTile:平铺模式,通过重复显示UIEdgeInsets指定的矩形区域来填充图片
UIEdgeInsets:可以通过设置UIEdgeInsets的left、right、top、bottom来分别指定左端盖宽度、右端盖宽度、顶端盖高度、底端盖高度
057B6B77-4C1C-4274-960A-77068C92C079.png拉伸其实就是拉伸中间的正方型,这样才能保证整个图片不变形
CGFloat margin = 5;
UIImage *image = [UIImage imageNamed:@"message_backGround"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100, [UIScreen mainScreen].bounds.size.width- 20, 140)];
UIEdgeInsets insets = UIEdgeInsetsMake((image.size.height / 2) - margin, (image.size.width / 2) - margin, (image.size.height / 2) - margin, (image.size.width / 2) - margin);
image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeTile];
imageView.image = image;
[self.view addSubview:imageView];
网友评论