【问题标题】:UIButton setImage vs setBackgroundImage with resizable imagesUIButton setImage vs setBackgroundImage 与可调整大小的图像
【发布时间】:2013-08-14 00:10:06
【问题描述】:

我正在尝试使用可调整大小的图像作为按钮背景。

UIImage *image = [UIImage imageNamed:@"btn_prev_secondary"];
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(30.0f, 25.0f, 29.0f, 9.0f);
UIImage *resizeableImage = [image resizableImageWithCapInsets:edgeInsets];

// What I'd like to do:
[self.backButton setBackgroundImage:resizeableImage forState:UIControlStateNormal];

// This works (but doesn't do what I want)
// [self.backButton setImage:resizeableImage forState:UIControlStateNormal];

// Also works (also doesn't do what I want)
// [self.backButton setImage:image forState:UIControlStateNormal];

当我运行这段代码时,我的按钮背景设置正确,但出现各种日志错误:

Aug 13 17:05:25 MyApp <Error>: CGContextSaveGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextSetBlendMode: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextSetAlpha: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextTranslateCTM: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextScaleCTM: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextGetCTM: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextSaveGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextClipToRect: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextDrawTiledImage: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextRestoreGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextGetCTM: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextSaveGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextClipToRect: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextDrawTiledImage: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextRestoreGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextGetCTM: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextSaveGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextClipToRect: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextDrawTiledImage: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextRestoreGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextGetCTM: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextSaveGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextClipToRect: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextDrawTiledImage: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextRestoreGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextGetCTM: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextSaveGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextClipToRect: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextDrawTiledImage: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextRestoreGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextGetCTM: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextSaveGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextClipToRect: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextDrawTiledImage: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextRestoreGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextRestoreGState: invalid context 0x0

这是 Cocoa 错误还是我做错了什么(以及如果我正在做的事情如何纠正)?

【问题讨论】:

  • 什么是self.backButton?
  • self.backButton 是在故事板上定义的 UIButton。
  • 我复制并粘贴了您的代码,对我来说效果很好。你怎么知道是这段代码导致了错误?
  • 有趣!我想是这段代码,因为如果我更改或注释该特定行,我看不到错误 - 只有在我使用可调整大小的图像时才会发生。也许我对视图所做的事情与这个问题有关——我从情节提要中对其进行实例化,然后将其动画推送到导航堆栈上。此导航控制器位于占据屏幕子部分的容器视图中。我会试着去掉复杂性,看看它什么时候开始抱怨。

标签: ios objective-c cocoa uibutton core-graphics


【解决方案1】:

可能是因为您设置了错误的edgeInsets。请确保您的edgeInsets.left + edgeInsets.right 小于您的image.size.widthedgeInsets.top + edgeInsets.bottom 小于image.size.height

或者您可能想查看this question&answer

【讨论】:

  • 啊,大概就是这样(我明天试试)。我的顶部 + 底部 == 高度,因为我不希望它垂直平铺。 FWIW,它确实显示正确。
猜你喜欢
  • 2021-07-01
  • 2018-03-16
  • 2021-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-18
  • 2012-05-02
相关资源
最近更新 更多