gpt4 book ai didi

iphone - iphone 中的 jpg 图像和 2x 图像

转载 作者:IT王子 更新时间:2023-10-29 08:01:20 25 4
gpt4 key购买 nike

我正在开发一个 iphone 应用程序,目标是 iOS 4.0 或更高版本。我正在尝试向 UIImageView 添加图像,而图像的格式为 jpeg。这是我到目前为止所做的..

UIImageView *bgImageView            =   [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 246.0)];
bgImageView.image = [UIImage imageNamed:@"background"];
[self.view addSubview:bgImageView];
[bgImageView release];

我添加了两张图片,

  1. background.jpg(正常 1x 图片)
  2. background@2x.jpg(用于 2x/视网膜显示)。

但是当我构建并运行时,一切运行正常,只是没有背景。我所有的其他小部件都在那里,但是有一个白色背景并且没有显示图像。

接下来,我为我的背景创建了两个 png 图像,将其添加到项目中并运行。这次显示了我的背景。我很困惑,因为根据 documentation jpg 图像可以通过 UIImage 在 iphone 中使用。

我再次尝试使用我的 jpg 图片并将上面的代码更改为这个

UIImageView *bgImageView            =   [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 246.0)];
bgImageView.image = [UIImage imageNamed:@"background.jpg"]; //added extension
[self.view addSubview:bgImageView];
[bgImageView release];

当我运行上面的代码时,显示背景图像,但未加载 2x 图像。这表明我的 jpg 图像没有问题,它不是罪魁祸首。因此,当我只提供没有扩展名的图像名称时,png 文件(1x 和 2x)会加载到相应的设备上,而根本不会加载 jpg 文件。看起来像一个错误(在 sdk 或文档中)。 iOS 4 迎来了美好的一年。为什么没有人注意到它?除了将所有图像转换为 png 之外,我还有其他选择吗?

编辑:

好吧,问题两天了,答案为零。我已经将我所有的图像转换为 png,并且我的项目的这一部分已经完成(因为我的项目等不及了..)。尽管如此,为了我自己,也为了 future 遇到这个问题的每个人,我想更多地了解这个问题。至少有人可以确认这确实是一个 apple bug。我开始悬赏,这样更多的人可以看到这个帖子。

再次编辑

此赏金事件还剩 2 天结束。感谢所有回复的人。但让我澄清一件事。我已经说过我已经将所有图像转换为 png 并且我继续前进。我只是想确认 imageNamed 以相同方式处理 jpg 和 png 文件。文档肯定会说它会(除非 Apple 将它写在 UIImage 文档以外的地方)。如果文档是正确的,那么我的代码由于两个可能的原因而失败

  1. 这是 imageNamed 中的错误。如果这是原因,那么除了提交错误报告、将我的图像更改为 png 或编写包装器(就像 Chandan 在他的回答中所说的那样)并继续前进之外,我别无选择。
  2. 我使用的 jpeg 图片有问题。好吧,我不是 photoshop 专家。但我用过RGBA 图像。如果图像很重要,我准备好询问我的设计师 friend 并提供更多信息。

还有一件事。 This帖子也只是讲述了这个问题。但他不知道背后的原因。所以我并不孤单。

编辑:结束

嗯,这与图像本身有关。我在谷歌上搜索并下载了一些示例 jpeg 图像并使用它。其中一些正确显示,而另一些则没有。由于边界时间到了,我把边界给了 PengOne,他试图重现这个错误并成功地加载了没有扩展名的 jpg 图像,这让我可以处理更多图像。感谢所有试图提供帮助的人。

最佳答案

根据UIImage类引用:

Discussion

This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

On a device running iOS 4 or later, the behavior is identical if the device’s screen has a scale of 1.0. If the screen has a scale of 2.0, this method first searches for an image file with the same filename with an @2x suffix appended to it. For example, if the file’s name is button, it first searches for button@2x. If it finds a 2x, it loads that image and sets the scale property of the returned UIImage object to 2.0. Otherwise, it loads the unmodified filename and sets the scale property to 1.0. See iOS Application Programming Guide for more information on supporting images with different scale factors. Special

Considerations

On iOS 4 and later, the name of the file is not required to specify the filename extension. Prior to iOS 4, you must specify the filename extension.

由于您的目标是 iOS 4.0 及更高版本,因此您不需要需要文件扩展名。我试图重现这个潜在的错误,但它在没有文件扩展名的情况下按预期工作。

以下是关于在您的应用中创建此效果可能出错的一些想法:

  1. 如果您在某个时候更改了图像,则问题可能来自缓存。

  2. 如果您选择不使用文件扩展名,并且您有“background.jpg”和“background.png”这两个选项,则首选似乎是使用“.png”文件。

  3. 如果您在首次运行应用程序后将目标设置为 iOS 4.0 及更高版本,则需要文件扩展名并且图像可能已被缓存为空白,这又回到了理论 1。

这是我能做到的最好的了。

关于iphone - iphone 中的 jpg 图像和 2x 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6595365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com