如何让 [UIImage imageWithContentsOfFile:] 和高分辨率图像正常工作
正如许多人抱怨的那样,Apple Retina Display SDK 中似乎存在一个错误,并且 imageWithContentsOfFile 实际上不会自动加载 2x 图像。
我偶然发现了一篇很好的文章,如何制作一个检测 UIScreen 比例因子并正确加载低或高分辨率图像的函数( http://atastypixel.com/blog/uiimage-solution-independence-and-the-iphone-4s-retina-display/ ) ,但该解决方案加载 2x 图像,并且图像的比例因子仍然设置为 1.0,这会导致 2x 图像缩放 2 倍(因此,比它看起来的样子大 4 倍)
imageNamed 似乎准确地加载了低值和高分辨率图像,但对我来说没有选择。
有没有人有一个不使用自动加载 imageNamed 或 imageWithContentsOfFile 来加载低/高分辨率图像的解决方案? (或最终解决如何使 imageWithContentsOfFile 正确工作)
As many people are complaining it seems that in the Apple SDK for the Retina Display there's a bug and imageWithContentsOfFile actually does not automatically load the 2x images.
I've stumbled into a nice post how to make a function which detects UIScreen scale factor and properly loads low or high res images ( http://atastypixel.com/blog/uiimage-resolution-independence-and-the-iphone-4s-retina-display/ ), but the solution loads a 2x image and still has the scale factor of the image set to 1.0 and this results to a 2x images scaled 2 times (so, 4 times bigger than what it has to look like)
imageNamed seems to accurately load low and high res images, but is no option for me.
Does anybody have a solution for loading low/high res images not using the automatic loading of imageNamed or imageWithContentsOfFile ? (Or eventually solution how to make imageWithContentsOfFile work correct)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果指定绝对路径,
[UIImage imageWithContentsOfFile:]
不会加载 @2x 图形。这是一个解决方案:
归功于 Christof Dorner 对于他的简单解决方案(我修改并粘贴在这里)。
[UIImage imageWithContentsOfFile:]
doesn't load @2x graphics if you specify an absolute path.Here is a solution:
Credit goes to Christof Dorner for his simple solution (which I modified and pasted here).
我已经为这个问题开发了一个临时解决方法。
它使用方法调配来替换 UIImage 的“imageWithContentsOfFile:”方法的行为。
它在 iPhone/iPod 视网膜前/后工作正常。
不确定 iPad 的情况。
希望这有帮助。
I've developed a drop-in workaround for this problem.
It uses method swizzling to replace the behavior of the "imageWithContentsOfFile:" method of UIImage.
It works fine on iPhones/iPods pre/post retina.
Not sure about the iPad.
Hope this is of help.
增强 Lisa Rossellis 的答案,将视网膜图像保持在所需的尺寸(而不是放大):
Enhancing Lisa Rossellis's answer to keep retina images at desired size (not scaling them up):
从 iOS 4.1 及更高版本开始,
imageWithContentsOfFile
可以正常工作(考虑具有正确比例的 @2x 图像)。imageWithContentsOfFile
works properly (considering @2x images with correct scale) starting iOS 4.1 and onwards.我们刚刚在工作中遇到了这个。
这是我的解决方法,似乎有效:
We just ran into this here at work.
Here is my work-around that seems to hold water:
好的,迈克尔在这里找到了实际的解决方案:
http://atastypixel.com/blog/ uiimage-resolution-independence-and-the-iphone-4s-retina-display/
他发现 UIImage 有方法“initWithCGImage”,该方法也将比例因子作为输入(我猜这是唯一可以使用的方法)设置自己的比例因子)
这似乎效果很好,您可以自定义加载高分辨率图像并只需将比例因子设置为 2.0
imageWithContentsOfFile 的问题是,由于它当前无法正常工作,因此我们不能信任它即使它已修复(因为某些用户的设备上仍然有较旧的 iOS)
Ok, actual solution found by Michael here :
http://atastypixel.com/blog/uiimage-resolution-independence-and-the-iphone-4s-retina-display/
He figured out that UIImage has the method "initWithCGImage" which also takes a scale factor as input (I guess the only method where you can set yourself the scale factor)
And this seems to work great, you can custom load your high res images and just set that the scale factor is 2.0
The problem with imageWithContentsOfFile is that since it currently does not work properly, we can't trust it even when it's fixed (because some users will still have an older iOS on their devices)