我需要拉伸图像左右中心半圆保持原样。中心还需要半圈
我尝试过切片概念,并尝试下面的代码
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];
但它对我不起作用
请帮帮我。提前谢谢。
答案 0 :(得分:3)
您正在使用功能,请使用- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight
代替- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
。
设置顶部左帽以拉伸图像,如下面的代码所述。
<强>目标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];
<强>夫特强>
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)
使用以下功能也可以实现相同的结果。
<强>目标C 强>
UIImage *stretchedImage = [image resizableImageWithCapInsets:
UIEdgeInsetsMake(50, 50, 0, 50)];
<强>夫特强>
var stretchedImage = image?.resizableImage(withCapInsets: UIEdgeInsets(top: 50, left: 50, bottom: 0, right: 50), resizingMode: .stretch)
注意:保持可伸缩图像尽可能小,否则无法使用较小的图像容器(ImageView,Button等)正确拉伸。你可以减少高度和现有图像的宽度。
答案 1 :(得分:0)
试试这个
ubuntu:latest top