在 iPad 上强制加载 x2 版本图像时图像模糊

发布于 2024-12-29 11:33:21 字数 928 浏览 3 评论 0原文

我正在尝试手动强制 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

土豪我们做朋友吧 2025-01-05 11:33:21

这只是在黑暗中拍摄的,但是尝试将 init 方法的比例更改为 2。

NSString *filename = [myImage filenameWithSuffix:@"@2x"];
UIImage *image = [[UIImage alloc] initWithCGImage:[[UIImage imageWithData:[NSData dataWithContentsOfFile:filename]] CGImage]
                                            scale:2.0
                                      orientation:UIImageOrientationUp];

UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

我可以使用以下命令在 iPad 上很好地加载 @2x 图像:(

UIImage *image = [UIImage imageNamed:@"fileName@2x"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView sizeToFit];
[self.view addSubview:imageView];

因为我使用了 imageNamed ,并且图像是 .png,我没有将 .png 附加到文件名,因为该方法假定它)

This is just a shot in the dark, but try changing the scale on the init method to 2.

NSString *filename = [myImage filenameWithSuffix:@"@2x"];
UIImage *image = [[UIImage alloc] initWithCGImage:[[UIImage imageWithData:[NSData dataWithContentsOfFile:filename]] CGImage]
                                            scale:2.0
                                      orientation:UIImageOrientationUp];

UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

I was able to load a @2x image just fine on the iPad using this:

UIImage *image = [UIImage imageNamed:@"fileName@2x"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView sizeToFit];
[self.view addSubview:imageView];

(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)

听风吹 2025-01-05 11:33:21

我已经解决了这个问题,但我很不好意思地说这是由于像素对齐造成的。

我发现iOS模拟器可以突出显示未对齐的图像,这证明图像确实未对齐。然后,我从图像向上记录每个视图的位置,以便找到导致问题的视图。这段代码多次证明了它的价值:

    NSLog(@"View details");
    UIView *view = theImageView;
    while (view)
    {
        NSLog(@"%@", view);
        view = [view superview];
    }

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:

    NSLog(@"View details");
    UIView *view = theImageView;
    while (view)
    {
        NSLog(@"%@", view);
        view = [view superview];
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文