【问题标题】:Loading a png into a UIImageView iOS将 png 加载到 UIImageView iOS
【发布时间】:2012-07-18 19:17:38
【问题描述】:

我有两张需要相互叠加的图像,它们都是 png 图像(因为我需要能够使它们透明)。但是,当我将它们加载到我的 xib 文件中的 UIImage 视图中时,它们都不会显示!当我尝试使用相同图像的 jpg 格式时,它可以正常工作,但是因为 jpg 不支持透明度,所以我需要的叠加效果丢失了。如何让 png 图像实际显示在窗口中?

【问题讨论】:

  • 界面生成器的截图好吗?
  • 存在iOS无法显示的png文件的情况。所以当你在xib文件中使用then的时候,你也已经在项目中包含了图像,并且图像被包含在了目标中(你看了右边的pan)。在代码中,尝试获取图像 [UIImage imageNamed:foo.png];看看它是否加载。尝试使用刚刚从 imageNamed 获得的图像配置 imageViews.image。尝试在 Mac 上的“预览”中打开图像,以验证您可以在那里看到它们。
  • 如果你想使用 alpha 覆盖图像,你应该取消选中 opaque

标签: objective-c ios cocoa-touch uiimageview png


【解决方案1】:

这是一种从代码中比从界面“Crappy”构建器中更容易完成的任务:

CGRect imageFrame = CGRectMake(x, y, width, height);

UIImage *image1 = // however you obtain your 1st image
UIImage *image2 = // however you obtain your 2nd image

UIImageView *imgView1 = [[UIImageView alloc] initWithImage:image1];
// Adjust the alpha of the view
imgView1.alpha = 1.0f; // This is most advisably 1.0 (always)
imgView1.backgroundColor = [UIColor clearColor];
imgView1.frame = imageFrame;

UIImageView *imgView2 = [[UIImageView alloc] initWithImage:image2];
// Adjust the alpha of the view
imgView1.alpha = 0.5f; // or whatever you find practical
imgView1.backgroundColor = [UIColor clearColor];
imgView2.frame = imageFrame;

// Assume a view controller
[self.view addSubview:imgView1];
[self.view addSUbview:imgView2]; // add the image view later which you wanna be on the top of the other one

// If non-ARC environment, we need to take care of the percious RAM
[imgView1 release];
[imgView2 release];

【讨论】:

  • 我将图像重新添加到项目中并以编程方式构建它,现在它可以工作了!谢谢!
  • @JulianColtea 没问题。请注意——我有几个著名的 iOS 项目,但我从未使用过 Interface Builder。
【解决方案2】:

尝试在 photoshop 或 pixelmator 等照片编辑器中打开您的 png 图像,然后将其再次保存为非隔行扫描(在 png 的保存选项中)。

【讨论】:

    猜你喜欢
    • 2014-12-02
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    • 2020-10-02
    相关资源
    最近更新 更多