我需要拉伸(stretch)图像左右中心半圆保持原样。中间还需要半圆
我试过切片的概念,也试过下面的代码
UIImage *image = self.imgBGBottom.image;
CGFloat capWidth = floorf(image.size.width / 2) - 50;
CGFloat capHeight = 0;
UIImage *capImage = [image resizableImageWithCapInsets:
UIEdgeInsetsMake(capHeight, capWidth, capHeight, capWidth)];
[self.imgBGBottom setImage:capImage];
但它对我不起作用
请帮帮我。提前致谢。
最佳答案
您正在使用函数,请使用 - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight
而不是 - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
.
如以下代码所述,设置左上角的 cap 以拉伸(stretch)图像。
objective-C
UIImage *image = [UIImage imageNamed:@"test"];
CGFloat capTop = 50; // top cap to keep half round as is
CGFloat capLeft = 5; // To repeat it or stretch equally.
UIImage *capImage = [image stretchableImageWithLeftCapWidth:capLeft topCapHeight:capTop];
swift
let image = UIImage(named: "stretchableImage")
let capTop:Int = 50; // top cap to keep half round as is
let capLeft:Int = 5; // To repeat it or stretch equally.
let capImage = image?.stretchableImage(withLeftCapWidth: capLeft, topCapHeight: capTop)
替代网络解决方案
同样的结果也可以使用下面的函数来实现。
objective-C
UIImage *stretchedImage = [image resizableImageWithCapInsets:
UIEdgeInsetsMake(50, 50, 0, 50)];
swift
var stretchedImage = image?.resizableImage(withCapInsets: UIEdgeInsets(top: 50, left: 50, bottom: 0, right: 50), resizingMode: .stretch)
Note : Keep stretchable image as small as possible otherwise it will not stretch properly with smaller image container(ImageView, Button etc.). You can reduce height & width of your existing image.
关于ios - 如何在 iOS 中切片图像和拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39284749/