如何在这样的代码中绘制可调整大小的UIImage



我想为UISegmentedControl使用自定义背景图像,比如"查找我的朋友"。以下是它们可调整大小的图像的样子:

来源http://feedzr.com/source.png来源http://feedzr.com/inwork.png

如何在Core Graphics中创建复杂的阴影效果?

编辑

下图是在真正的UISegmentedControl中使用皮革背景时上面的样子。在可调整大小的图像中有很多效果,比如:底部光泽、顶部内部阴影和从上到下的部分梯度。

我就是不明白&这张图片中使用了什么效果。我不是在问如何使用UIEdgeInsets。

这只是两张图片。一个有阴影一个没有…

你必须用CapInsets创建这些图像,使它们可以调整的大小

例如

UIImage *buttonImage = [[UIImage imageNamed:@"yourImage"]  
   resizableImageWithCapInsets:UIEdgeInsetsMake(1, 11, 0, 20)];

要了解UIEdgeInsets的工作原理,请阅读:UIEdgeInsetsMake是如何工作的?

或者在苹果文档中:

UIEdgeInsetsMake
Creates an edge inset for a button or view.
UIEdgeInsets UIEdgeInsetsMake (
   CGFloat top,
   CGFloat left,
   CGFloat bottom,
   CGFloat right
);
Parameters
top
The inset at the top of an object.
left
The inset on the left of an object
bottom
The inset on the bottom of an object.
right
The inset on the right of an object.
Return Value
An inset for a button or view
Discussion
An inset is a margin around the drawing rectangle where each side (left, right, top, and bottom) can have a different value.
Availability
Available in iOS 2.0 and later.
See Also

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html