且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在iPhone应用程序中仅缩放图像的特定部分?

更新时间:2023-01-15 18:23:17

听起来你想做一个9切片缩放形式或3-切片缩放。假设您有以下图片:

It sounds like you want to do a form of 9-slice scaling or 3-slice scaling. Let's say you have the following image:

你想让它看起来像这样:

and you want to make it look like this:

(对角线末端部分根本不伸展,顶部和底部部分伸展水平,左边和右边的部分垂直伸展)

(the diagonal end pieces do not stretch at all, the top and bottom pieces stretch horizontal, and the left and right pieces stretch vertical)

要做到这一点,在iOS中使用 -stretchableImageWithLeftCapWidth:topCapHeight: 4.x及更早版本,或 -resizableImageWithCapInsets:从iOS 5开始。

To do this, use -stretchableImageWithLeftCapWidth:topCapHeight: in iOS 4.x and earlier, or -resizableImageWithCapInsets: starting with iOS 5.

UIImage *myImage = [UIImage imageNamed:@"FancyButton"];
UIImage *myResizableImage = [myImage resizableImageWithCapInsets:UIEdgeInsetsMake(21.0, 13.0, 21.0, 13.0)];
[anImageView setImage:myResizableImage]

为了帮助可视化缩放,这里有一张图片显示以上上限:

To help visualize the scaling, here is an image showing the above cap insets: