【问题标题】:Xcode render the .PNG images all whiteXcode 将 .PNG 图像全部渲染为白色
【发布时间】:2012-02-10 21:16:41
【问题描述】:

我正在将 .PNG 导入 xcode。图像具有透明度。当它从 ID 渲染到按钮上时,除了透明度之外,图像的所有内容都变为白色。

这是一个已知问题吗?是因为我保存 .PNG 的方式吗?

感谢您的帮助!

【问题讨论】:

  • .png 在其他地方查看时是否正确(例如预览)?如果您发布相关代码,可能更容易发现问题。
  • 等一下 - 你说的是 UIButton 还是 UIBarButtonItem?听起来像后者……如果将图像加载到 UIImageView 中,图像是否正确显示?
  • 是的,图像在预览中看起来很好(在 xcode 中导入图像之前),但在界面构建器中,我在按钮属性上设置图像
  • 是的,它是一个 UIBarButtonItem,如果它是 UIImageView,它确实可以正确显示

标签: xcode png transparency


【解决方案1】:

这就是图像在 UIBarButtonItems 和 UITabBarItems 上的工作方式:您提供的图像仅用作透明蒙版。您可以使用 UIImageView/UIButton 来显示您的图像(正如 AtkinsonCM 所建议的那样),或者创建一个用作遮罩的图像。

我个人认为通常最好遵守 Apple 的规则,除非您有令人信服的理由不这样做(例如,用户体验会得到显着改善)。

【讨论】:

    【解决方案2】:

    要在 UIBarButtonItem 中显示图像,您需要使用该图像创建一个 UIButton,然后将该按钮添加到 UIBarButtonItem。否则,正如您所提到的,您只会得到大纲。

    所以:

    // Initialize the UIButton with your image
    UIImage *myImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"someImage" ofType:@"png"]];
    UIButton *myButton = [[UIButton alloc] init];
    [myButton setImage:myImage forState:UIControlStateNormal];
    
    //Then create the UIBarButton
    UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
    

    【讨论】:

      猜你喜欢
      • 2014-07-30
      • 2012-07-27
      • 1970-01-01
      • 1970-01-01
      • 2012-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多