UIImage如何只拉伸部分图片

4

假设我有这个精灵:

mbm
bcb
mbm

每个字母都代表一部分。(m:边距; b:边框; c:居中)

我想要一个类,能够重复b和c直到填满视图,这样我就可以得到如下效果:

mbbbbbbbbbbbbbm
bcccccccccccccb
bcccccccccccccb
bcccccccccccccb
mbbbbbbbbbbbbbm

已经有类似的东西了吗? 如果没有,有什么实现的想法吗?

2个回答

8
你能用这种方法实现吗? -(UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight (请参见苹果UIImage类参考

我忘了提到...就像我说的,这是四个角落...那种方法只解决左上角。 - Diego Torres
1
那个方法可以实现你想要的效果。虽然你只指定了顶部和左侧,但它只会拉伸刚好超过端点的一个像素条。这样的效果是只有中间部分被拉伸。 - Dancreek

2
我们可以使用以下代码拉伸图像:- 在这里,我们需要m..m的大小相同,因此我们拉伸中间部分。
UIImage *image = [UIImage imageNamed:@"img_loginButton.png"];
    UIEdgeInsets edgeInsets;
    edgeInsets.left = 3.0f; //Assume it is the pixel for starting 'm'
    edgeInsets.top = 0.0f;
    edgeInsets.right = 3.0f; //Assume it is the pixel for Ending 'm'
    edgeInsets.bottom = 0.0f;
    image = [image resizableImageWithCapInsets:edgeInsets];
//Use this image as your controls image

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接