在 iPad 上强制加载 x2 版本图像时图像模糊
我正在尝试手动强制 iOS 加载文件图像文件的 2x 版本。我这样做是为了节省资源 - 我需要 iPhone 版本的图形,并且我想在 iPad 版本中以 1:1 的比例使用它。
为此,我无法使用 initWithContentsOfFile,因为即使我将 x2 添加到文件名中,iOS 也会为我提供 x1 版本。相反,我使用这段代码:
NSString *filename = [myImage filenameWithSuffix:@"@2x"];
UIImage *image = [[UIImage alloc] initWithCGImage:[[UIImage imageWithData:[NSData dataWithContentsOfFile:filename]] CGImage]
scale:1.0
orientation:UIImageOrientationUp];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
有效。我 100% 确定它正在选择 x2 版本(我更改了该图形以确保)。
然而,当它以正确的尺寸显示在屏幕上时,它是模糊的。事实上,看起来 x1 版本放大了 200%,所以 iOS 对我的形象做了一些奇怪的事情。
我已确保图像绘制在像素边界上。
更新:
即使我创建另一个不带 x2 后缀的图像文件并使用 initWithContentsOfFile 加载它,它仍然是模糊的。所以这与它的加载方式无关。
除了像素错位之外,还有哪些因素会导致图像质量下降?
请注意,我已截取屏幕截图并在 Photoshop 中将图像与原始图像进行比较。它的大小完全相同,因此缩放不是问题。
I'm trying to manually force iOS to load a 2x version of an file image file. I'm doing this to save resources - I need that graphic for the iPhone version, and I want to use it at a scale of 1:1 for the iPad version.
To do this I can't use initWithContentsOfFile because iOS gives my the x1 version even if I add x2 onto the filename. Instead, I use this code:
NSString *filename = [myImage filenameWithSuffix:@"@2x"];
UIImage *image = [[UIImage alloc] initWithCGImage:[[UIImage imageWithData:[NSData dataWithContentsOfFile:filename]] CGImage]
scale:1.0
orientation:UIImageOrientationUp];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
That works. I'm 100% sure that it's picking up the x2 version (I changed that graphic to make sure).
However, when it's displayed on the screen, at the correct size, it's blurry. In fact, it looks like the x1 version blown up to 200%, so iOS is doing something odd to my image.
I have ensured that the images are being drawn on pixel boundaries.
UPDATE:
Even if I create another image file without the x2 suffix and use initWithContentsOfFile to load it, it's still blurred. So this has nothing to do with how it's being loaded.
Other than pixel misalignment, what can cause image degradation?
Note that I've taken a screenshot and compare the image to the original in Photoshop. It's exactly the same size, so scaling isn't the issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这只是在黑暗中拍摄的,但是尝试将 init 方法的比例更改为 2。
我可以使用以下命令在 iPad 上很好地加载 @2x 图像:(
因为我使用了
imageNamed
,并且图像是 .png,我没有将 .png 附加到文件名,因为该方法假定它)This is just a shot in the dark, but try changing the scale on the init method to 2.
I was able to load a @2x image just fine on the iPad using this:
(Since I used
imageNamed
, and the image was a .png, I didn't append .png to the filename because it is assumed by the method)我已经解决了这个问题,但我很不好意思地说这是由于像素对齐造成的。
我发现iOS模拟器可以突出显示未对齐的图像,这证明图像确实未对齐。然后,我从图像向上记录每个视图的位置,以便找到导致问题的视图。这段代码多次证明了它的价值:
I've solved this issue, and I'm embarrassed to say that it was due to pixel alignment.
I discovered that the iOS simulator can highlight misaligned images, and that proved that the images were indeed misaligned. I then logged the position of every view from the image upwards in order to find the one that was causing the problem. This snippet proved its weight in gold several times: