处理图片拉伸变形的三种方法
2017-02-03 本文已影响1657人
AlexanderZhu
原始图片.png
UI给的图片入上图,如果按照如下的代码。
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(60, CGRectGetMaxY(imageView.frame)+10, self.view.frame.size.width - 120, 42)];
imageView2.image = image;
[self.view addSubview:imageView2];
8486547B-1EFD-4F61-B6D0-26C8553C7A8D.png
图片就会拉伸变形成入上图所示。有三种方法可以处理图片的拉伸变形问题。
- 第一种方法
<code>
UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(60, CGRectGetMaxY(imageView2.frame)+10, self.view.frame.size.width - 120, 42)];
imageView3.image = [image stretchableImageWithLeftCapWidth:20 topCapHeight:0];
[self.view addSubview:imageView3];
</code> - 第二种方法
<code>
UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(60, CGRectGetMaxY(imageView3.frame)+10, self.view.frame.size.width - 120, 42)];
imageView4.image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(0, 20, 0, 20)];
[self.view addSubview:imageView4];
</code> - 第三种方法
<code>
UIImageView *imageView5 = [[UIImageView alloc] initWithFrame:CGRectMake(60, CGRectGetMaxY(imageView4.frame)+10, self.view.frame.size.width - 120, 42)];
imageView5.image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(0, 20, 0, 20) resizingMode:UIImageResizingModeStretch];
[self.view addSubview:imageView5];
</code>
效果如下所示
Paste_Image.png