Xcode 图像切片 - 拉伸图像的外部但保留中心区域
Xcode image slicing - stretch the outsides of the image but preserve an area in the centre
我想在 Xcode 中使用图像切片来生成一个可调整大小的图像,其中心区域保持静态。这似乎在 Xcode 中是不可能的,因为它在每个方向上只允许一个拉伸区域和一个收缩区域。这是正确的吗?
有人知道解决这个问题的好方法吗?我需要将图像用作按钮,因此我无法将 UIImageView 彼此重叠排列,除非我通过触摸,这会变得有点混乱。
非常感谢。
由于 stretchableImageWithLeftCapWidth
在 iOS 5.0 中被弃用,您可以使用 resizableImageWithCapInsets
。检查 Apple Documentation,它指出,
You use this method to add cap insets to an image or to change the existing cap insets of an image. In both cases, you get back a new image and the original image remains untouched. For example, you can use this method to create a background image for a button with borders and corners: when the button is resized, the corners of the image remain unchanged, but the borders and center of the image expand to cover the new size.
还有一个方法也可以,resizableImage(withCapInsets:resizingMode:)
如果要设置resizingMode
你可以这样做,
UIImage *image = [UIImage imageNamed:@"yourImageName"];
UIImage *streachedImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)]; //Edgeinsect that you want
// OR
UIImage *streachedImage2 = [image resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5) resizingMode:UIImageResizingModeStretch];
// OR
UIImage *streachedImage3 = [image resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5) resizingMode:UIImageResizingModeTile];
查看 Documentation 了解更多详情。
更新:(根据评论)
您只需将 backgroundImage
和 image
都设置到您的按钮即可。您还可以设置 image inset
来管理您的 image
的位置。
我想在 Xcode 中使用图像切片来生成一个可调整大小的图像,其中心区域保持静态。这似乎在 Xcode 中是不可能的,因为它在每个方向上只允许一个拉伸区域和一个收缩区域。这是正确的吗?
有人知道解决这个问题的好方法吗?我需要将图像用作按钮,因此我无法将 UIImageView 彼此重叠排列,除非我通过触摸,这会变得有点混乱。
非常感谢。
由于 stretchableImageWithLeftCapWidth
在 iOS 5.0 中被弃用,您可以使用 resizableImageWithCapInsets
。检查 Apple Documentation,它指出,
You use this method to add cap insets to an image or to change the existing cap insets of an image. In both cases, you get back a new image and the original image remains untouched. For example, you can use this method to create a background image for a button with borders and corners: when the button is resized, the corners of the image remain unchanged, but the borders and center of the image expand to cover the new size.
还有一个方法也可以,resizableImage(withCapInsets:resizingMode:)
如果要设置resizingMode
你可以这样做,
UIImage *image = [UIImage imageNamed:@"yourImageName"];
UIImage *streachedImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)]; //Edgeinsect that you want
// OR
UIImage *streachedImage2 = [image resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5) resizingMode:UIImageResizingModeStretch];
// OR
UIImage *streachedImage3 = [image resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5) resizingMode:UIImageResizingModeTile];
查看 Documentation 了解更多详情。
更新:(根据评论)
您只需将 backgroundImage
和 image
都设置到您的按钮即可。您还可以设置 image inset
来管理您的 image
的位置。