xcode UIImage resizableImageWithCapInsets:未按预期工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10309064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:05:15  来源:igfitidea点击:

UIImage resizableImageWithCapInsets: not working as expected

objective-cxcodecocoa-touchios5uiappearance

提问by Brandon Cordell

I'm writing my first iOS app targeting the iOS 5.0+ platform. I'm using the UIAppearanceprotocol to customize the applications UI.

我正在编写我的第一个面向 iOS 5.0+ 平台的 iOS 应用程序。我正在使用该UIAppearance协议来自定义应用程序 UI。

I'm trying to change the backgrounds for UIBarButtonItemacross the entire application. Due to the fact that my UIBarButtonItem's may change size depending on the text or icon used I'm trying to utilize UIImage resizableImageWithCapInsets:with my background png.

我正在尝试更改UIBarButtonItem整个应用程序的背景。由于我的UIBarButtonItem's 可能会根据所使用的文本或图标改变大小,我正尝试将其UIImage resizableImageWithCapInsets:与我的背景 png一起使用。

I originally found the code I needed on Ray Wenderlich. Using the same exact code, with an image that's pretty close to the one used in the aforementioned tutorial, I'm getting weird results. Maybe it's just my inexperience with Cocoa Touch.

我最初在Ray Wenderlich上找到了我需要的代码。使用完全相同的代码,图像与上述教程中使用的图像非常接近,我得到了奇怪的结果。也许这只是我对 Cocoa Touch 的经验不足。

Here is the code I'm using.

这是我正在使用的代码。

DreamsAppDelegate.m - customizeAppearance:

DreamsAppDelegate.m - 自定义外观:

UIImage *btnBg = [[UIImage imageNamed:@"navBarButton-bg"] 
          resizableImageWithCapInsets:UIEdgeInsetsMake(0, 6, 0, 6)];

[[UIBarButtonItem appearance] setBackgroundImage:btnBg 
                                        forState:UIControlStateNormal 
                                      barMetrics:UIBarMetricsDefault];

Here is the png background image I'm trying to use

这是我尝试使用的 png 背景图像

png background

png 背景

And here is the result (in the simulator)

这是结果(在模拟器中)

result of trying to use resizableImageWithCapInsets:

尝试使用 resizableImageWithCapInsets 的结果:

回答by Dondragmer

The section of your image between the leftand right, and between the topand bottom, is tiled to fill the space needed by the image. Try UIEdgeInsetsMake(15, 6, 15, 6).

之间的图像的部分leftright之间,以及topbottom,平铺填补图像所需的空间。试试UIEdgeInsetsMake(15, 6, 15, 6)

To generalize, if your image has a continuous gradient, the repeated section should only be 1 pixel high. Since your button image is 31 pixels high, the topand bottom(the first and third arguments to UIEdgeInsetsMake) should add to 30. They don't have to be equal; UIEdgeInsetsMake(8, 6, 22, 6)will move the repeated section up, resulting in a paler background.

概括地说,如果您的图像具有连续渐变,则重复部分应该只有 1 像素高。由于您的按钮图像高 31 像素,因此topbottom( 的第一个和第三个参数UIEdgeInsetsMake)应加到 30。它们不必相等;UIEdgeInsetsMake(8, 6, 22, 6)会将重复的部分向上移动,从而使背景变淡。

Also, is the file you've attached the plain or the retina ('@2x') version of the image? The insets have to be within the size of the plain version.

另外,您附加的是图像的普通版本还是视网膜('@2x')版本的文件?插图必须在普通版本的大小范围内。

回答by Anonymous White

I have the exact same problem.

我有完全一样的问题。

Another thing you can do is to set the resizingMode of UIImageResizingModeStretch. It solved my problem.

您可以做的另一件事是设置 UIImageResizingModeStretch 的 resizingMode。它解决了我的问题。

Hopwever, it's not available in IOS5. So my solution would be to do as everyone else said. Realize that the tileable side should be just one pixel. Most buttons do not change much in the middle anyway.

但是,它在 IOS5 中不可用。所以我的解决方案是按照其他人说的去做。意识到可平铺的一侧应该只是一个像素。无论如何,大多数按钮在中间没有太大变化。

So, use this code:

所以,使用这个代码:

-(UIImage *) resizableImageWithCapInsets2: (UIEdgeInsets) inset
    {
        if ([self respondsToSelector:@selector(resizableImageWithCapInsets:resizingMode:)])
        {
            return [self resizableImageWithCapInsets:inset resizingMode:UIImageResizingModeStretch];
        }
        else
        {
            float left = (self.size.width-2)/2;//The middle points rarely vary anyway
            float top = (self.size.height-2)/2;
            return [self stretchableImageWithLeftCapWidth:left topCapHeight:top];
        }
    }