iphone和2x图像中的jpg图像 [英] jpg images in iphone and 2x images

查看:130
本文介绍了iphone和2x图像中的jpg图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发iphone应用并定位 iOS 4.0 或更高版本。我正在尝试将图像添加到UIImageView,图像是 jpeg格式。这就是我到目前为止所做的..

I am working on an iphone app and targeting iOS 4.0 or later. I am trying to add an image to UIImageView, and image is in jpeg format. This is what I have done so far..

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 / retina
    显示)。

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

But when I build and run, everything runs fine, except there is no background. All my other widgets are there, but there is a white background and image is not showing.

接下来,我为我的背景创建了两个png图像,将其添加到项目中并运行。这次是我的背景。我很困惑,因为根据文档 jpg图片可以用于iPhone和UIImage。

Next I created two png images for my background, added it to the project and ran. This time my background was shown. I was perplexed because according to documentation jpg image can be used in iphone with UIImage.

我再次尝试使用我的jpg图像并将上面的代码更改为此

I tried again with my jpg images and changed the code above to this

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之外,还有什么选择吗?

When I run the above code, background image is showing, but 2x images are not loaded. That shows my jpg image is fine, and it is not the culprit. So when I just give image name without extension, png files (both 1x and 2x)are loaded on respective devices, jpg files are not loaded at all. Looks like a bug (either in sdk or documentation). iOS 4 is here for a good year. why no one noticed it? Also is there any option for me other than to convert all my images to png?

编辑:

嗯,两天的问题,零回答。我已经将所有图像转换为png,并且我的项目的那部分已经完成(因为我的项目无法等待......)。尽管如此,为了我自己和为了将来面对这一切的每个人,我想更多地了解这个问题。至少有人可以确认这确实是一个苹果虫。我正在开始赏金,以便更多人可以看到这个帖子。

Well, two days in to the question, zero answers. I have already converted all my images to png, and that part of my project is done (as my project couldn't wait..). Still, for my own sake and for sake of everyone who faces this in future, I would like to know more about this problem. At least someone can confirm that this is indeed an apple bug. I am starting a bounty so that more people can see this thread.

再次编辑

此奖金剩余2天结束。感谢所有回复的人。但让我澄清一件事。我已经说过我已将所有图像转换为png而且我一直在移动。我只是想要一些确认 imageNamed 以同样的方式处理jpg和png文件。文档肯定会说(除非Apple已将其写入 UIImage 文档以外的其他地方)。如果文档是正确的,那么我的代码就失败了,原因有两个:

2 days remaining for this bounty to end. Thanks for everyone who responded. But let me clarify one thing. I have already said that I have converted all my images to png and I moved along. I just wanted some confirmation that imageNamed treats jpg and png files same way. Documentation certainly says it will(unless Apple has written it somewhere other than UIImage documentation). If documentation is correct then my code failed me because of two possible reasons


  1. 这是 <$中的错误C $ C> imageNamed 。如果这是原因,那么我没有太多的选择,除了提交错误报告,将我的图像更改为png或写一个包装(就像Chandan在他的回答中说的那样)并继续前进..

  2. 我使用的jpeg图像存在一些问题。好吧,我不是一个photoshop的家伙。但是我使用了
    RGBA jpg图像。如果图像确实重要,我准备问我的设计师
    朋友并提供更多信息。

  1. It is a bug in imageNamed. If this is the reason, then I dont have too much option other than file a bug report, change my images to png or write a wrapper(like Chandan has said in his answer) and move on..
  2. There is some problem with the jpeg image I used. Well I am not a photoshop guy. But I used RGBA jpg image. If image does matter I am ready to ask my designer friend and provide more info.

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

One more thing. This post also just tells about this problem. But he dont know the reason behind. So I am not alone.

编辑:结束

嗯,这是与图像本身有关的东西。我用Google搜索并下载了一些示例jpeg图像并使用它进行播放。其中一些显示正确,一些没有。由于限制时间到了,我正在向PengOne致敬,他试图重现这个bug并成功加载jpg图像而没有扩展,让我使用更多的图像。感谢所有试图提供帮助的人。

Well it is something related to the image itself. I googled and downloaded some sample jpeg images and played with it. Some of them shown up correctly and some doesn't. Since the boundy time is up, I am giving the boundy to PengOne who tried to reproduce the bug and successfully loaded jpg image without extension, there by making me to work with more images. Thanks for everyone who tried to help.

推荐答案

根据 UIImage 课程参考:


讨论

此方法在系统缓存中查找具有
指定名称的图像对象,并返回该对象(如果存在)。如果匹配的
图像对象不在缓存中,则此方法从指定文件加载图像
数据,缓存它,然后返回
结果对象。

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.

在运行iOS 4或更高版本的设备上,如果
设备的屏幕比例为1.0,则行为相同。如果屏幕的比例为2.0,
,则此方法首先搜索具有相同文件名
且附加了@ 2x后缀的图像文件。例如,如果文件的名称是
按钮,它首先搜索按钮@ 2x。如果找到2x,则加载
该图像并将返回的UIImage对象
的scale属性设置为2.0。否则,它会加载未修改的文件名并将比例
属性设置为1.0。有关支持具有不同比例因子的图像的更多
信息,请参阅iOS应用程序编程指南。特价

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

注意事项

在iOS 4及更高版本中,文件名不要求
指定文件扩展名。在iOS 4之前,您必须指定文件名
extension。

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及更高版本,应该需要文件扩展名。我试图重现这个潜在的错误,但它没有文件扩展名我的预期工作。

Since you're targeting iOS 4.0 and later, you should not need the filename extension. I tried to reproduce this potential bug, but it worked as expected for me without the filename extensions.

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

Here are some ideas for what may have gone wrong to create this effect in your app:


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

  1. It's possible that the problem comes from the cache if you changed your images at some point.

如果您选择不使用文件扩展名,并且您同时选择background.jpg和background.png作为选项,则首选项似乎是使用.png文件。

If you choose not to use the filename extensions, and you have both "background.jpg" and "background.png" as options, the preference appears to be to use the ".png" file.

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

If you set the target to iOS 4.0 and later after first running the app, then the filename extension would have been required and the image may have been cached blank, which leads back to theory 1.

这是我能做到的最好的。

That's the best I can make of this.

这篇关于iphone和2x图像中的jpg图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆