iphone - 拉伸(stretch) UIImage 同时保留角点

标签 iphone ios4 ios5 uiimage drawing

我正在尝试拉伸(stretch)导航箭头图像,同时保留边缘,以便中间拉伸(stretch)而末端固定。

这是我试图拉伸(stretch)的图像:

enter image description here

以下 iOS 5 代码允许图像在调整大小时拉伸(stretch)由 UIEdgeInsets 定义的图像的中心部分。

[[UIImage imageNamed:@"arrow.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 7, 15, 15)];

这会产生如下所示的图像(如果图像的框架设置为 70 像素宽):

enter image description here

这实际上是我想要的,但是resizableImageWithCapInsets仅支持 iOS 5 及更高版本。

在 iOS 5 之前,唯一类似的方法是 stretchableImageWithLeftCapWidth:topCapHeight但您只能指定顶部和左侧的插图,这意味着图像必须具有相同形状的边缘。

是否有与 iOS 5 的 ressizedImageWithCapInsets 方法相同的 iOS 4 调整图像大小的方法,或者其他方法?

最佳答案

你的假设是错误的:

Prior to iOS 5 the only similar method is stretchableImageWithLeftCapWidth:topCapHeight but you can only specify the top and left insets which means the image has to have equal shaped edges.

大写字母的计算方式如下 - 我将逐步介绍左大写字母,但相同的原则也适用于顶部大写字母。

假设您的图像宽度为 20 像素。

  • 左端宽度 - 这是图像左侧无法拉伸(stretch)的部分。在 stretchableImage 方法中,您为此发送值 10。
  • 可拉伸(stretch)部分 - 假定宽度为一个像素,因此为了更好的描述,它将是“11”列中的像素
  • 这意味着图像的剩余 9 像素有一个隐含的右上限 - 这也不会失真。

这取自 documentation

leftCapWidth

End caps specify the portion of an image that should not be resized when an image is stretched. This technique is used to implement buttons and other resizable image-based interface elements. When a button with end caps is resized, the resizing occurs only in the middle of the button, in the region between the end caps. The end caps themselves keep their original size and appearance.

This property specifies the size of the left end cap. The middle (stretchable) portion is assumed to be 1 pixel wide. The right end cap is therefore computed by adding the size of the left end cap and the middle portion together and then subtracting that value from the width of the image:

rightCapWidth = image.size.width - (image.leftCapWidth + 1);

关于iphone - 拉伸(stretch) UIImage 同时保留角点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8879817/

相关文章:

objective-c - UIViewReportBrokenSuperviewChain

iphone - 在我的代码的其余部分中访问输出的 'NSInteger'

ios5 - IOS - 垂直对齐 NavigationBar 中的后退按钮

ios - 使用 Xcode 6.2 在 iPad 1 (iOS 5.1.1) 上测试应用程序

iphone - iPhone 应用程序可以 root 身份运行吗?

iphone - iOS 开发 : What's a simple way to calculate the number of seconds that have passed between two events?

iOS 4.3 用户界面方向问题,状态栏有 20 个点

ios5 - UIView.frame 和 CALayer.frame 之间的关系是什么? (在 CATransform3D 之前和之后)

iphone - UITableViewCell 自定义重新排序控件

iphone - 在 iOS SDK 中模拟 Tab 键按下