iOS中以swift编程设置拉伸参数

所以,如果我们只想拉伸图像的一部分,不pipe是普通图像还是背景图像,我们都会在布局编辑器中使用以下设置:

在这里输入图像说明

你如何设置这些编程?

我正在使用Xcode 7.2.1

指定图像的大括号

您可以通过使用UIImage方法.resizableImageWithCapInsets(_:UIEdgeInsets, resizingMode: UIImageResizingMode)来设置拉伸细节。

宣言

 func resizableImageWithCapInsets(capInsets: UIEdgeInsets, resizingMode: UIImageResizingMode) -> UIImage 

描述

创build并返回一个新的图像对象与指定的帽子插图和选项。

一个新的图像对象与指定的帽子插图和resize模式。

参数

capInsets :用于大capInsets的值。

resizingMode :调整图像内部resizingMode的模式。


示例:使用指定的顶点插图进行自定义拉伸

作为一个例子,让我们尝试—以编程的方式—沿着它的宽度,正好在我的右腿(从视angular的左侧)拉伸我的(当前)个人资料图片,并保留图片的其余部分比例。 这可以相当于将某些button纹理的宽度拉伸到其内容的大小。

首先,让我们加载我们的原始图像foo.png作为UIImage对象:

 let foo = UIImage(named: "foo.png") // 328 x 328 

在这里输入图像说明

现在,使用.resizableImageWithCapInsets(_:UIEdgeInsets, resizingMode: UIImageResizingMode) ,我们将定义另一个UIImage实例,指定的cap insets(到我的右腿的中间),并将resize模式设置为.Stretch

 /* middle of right leg at ~ |-> 0.48: LEG :0.52 <-| along image width (for width normalized to 1.0) */ let fooWidth = foo?.size.width ?? 0 let leftCapInset = 0.48*fooWidth let rightCapInset = fooWidth-leftCapInset // = 0.52*fooWidth let bar = UIEdgeInsets(top: 0, left: leftCapInset, bottom: 0, right: rightCapInset) let fooWithInsets = foo?.resizableImageWithCapInsets(bar, resizingMode: .Stretch) ?? UIImage() 

请注意,上面的0.48字符对应于您在界面构build器中为Xinput的值,如上面问题中的图像(或在matt提供的链接中所述 )中所示。

继续往下看,我们最后把图片放在一个UIImageView ,并且让这个图片视图的宽度大于图片的宽度

 /* put 'fooWithInsets' in an imageView. as per default, frame will cover 'foo.png' size */ let imageView = UIImageView(image: fooWithInsets) /* expand frame width, 328 -> 600 */ imageView.frame = CGRect(x: 0, y: 0, width: 600, height: 328) 

由此产生的视图延伸原来的图像按规定,产生不成比例的长腿。

在这里输入图像说明

现在,只要图像的帧具有1:1 width:height比例( 328:328 ),则拉伸将是均匀的,就好像仅将任何图像适配到更小/更大的帧一样。 对于width值大于heighta:1 ,比率, a>1 )的任何帧,腿将开始不成比例地拉伸。


扩展匹配Interface Builder中的XwidthYheight伸展属性

最后,为了彻底地回答你的问题(我们只是在上面隐式地做了这个),我们可以利用在matt提供的链接中对XwidthYheight Interface Builder伸缩属性的详细解释来构造我们自己的UIImage扩展使用(显然)相同的属性,在扩展中翻译为cap insets:

 extension UIImage { func resizableImageWithStretchingProperties( XX: CGFloat, width widthProportion: CGFloat, Y: CGFloat, height heightProportion: CGFloat) -> UIImage { let selfWidth = self.size.width let selfHeight = self.size.height // insets along width let leftCapInset = X*selfWidth*(1-widthProportion) let rightCapInset = (1-X)*selfWidth*(1-widthProportion) // insets along height let topCapInset = Y*selfHeight*(1-heightProportion) let bottomCapInset = (1-Y)*selfHeight*(1-heightProportion) return self.resizableImageWithCapInsets( UIEdgeInsets(top: topCapInset, left: leftCapInset, bottom: bottomCapInset, right: rightCapInset), resizingMode: .Stretch) } } 

使用这个扩展,我们可以达到与上面相同的横向拉伸foo.png ,如下所示:

 let foo = UIImage(named: "foo.png") // 328 x 328 let fooWithInsets = foo?.resizableImageWithStretchingProperties( X: 0.48, width: 0, Y: 0, height: 0) ?? UIImage() let imageView = UIImageView(image: fooWithInsets) imageView.frame = CGRect(x: 0, y: 0, width: 600, height: 328) 

扩展我们的例子:拉伸宽度以及高度

现在,比如说我们想像上面那样拉伸我的右腿(沿着宽度),而且还要沿着图像的高度还有我的双手和左腿。 我们通过使用上面扩展中的Y属性来控制这个:

 let foo = UIImage(named: "foo.png") // 328 x 328 let fooWithInsets = foo?.resizableImageWithStretchingProperties( X: 0.48, width: 0, Y: 0.45, height: 0) ?? UIImage() let imageView = UIImageView(image: fooWithInsets) imageView.frame = CGRect(x: 0, y: 0, width: 500, height: 500) 

产生以下拉伸的图像:

在这里输入图像说明


这个扩展显然允许更加灵活地使用cap插入扩展(与使用Interface Builder相似的多function性),但是请注意,扩展在当前forms中不包含任何用户inputvalidation,所以由调用者来决定在正确的范围内使用参数。

最后,有关任何涉及图像及其坐标的操作的相关说明:

注意:图像坐标轴x (宽度)和y (高度)运行为

 x (width): left to right (as expected) y (height): top to bottom (don't miss this!)