动态加载视网膜图像到标签栏

发布于 2024-12-19 19:09:20 字数 634 浏览 0 评论 0原文

我在标签栏中使用的图像是动态的,我从互联网上加载它们。我检测我是否在视网膜显示屏上,并下载正确的图像并使用它们初始化选项卡栏。

正常图像工作正常。我的问题是视网膜图像。它们以大尺寸显示,比选项卡栏大,因此它们没有完全显示。

我想知道如何将视网膜图像加载到选项卡栏中并使它们看起来正常。

更新:

这是我用来从添加的每个视图控制器将图像加载到选项卡栏的代码:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithString:@"1.png"]];
    UIImage* image = [UIImage imageWithContentsOfFile:path];

图像是否有 @2x 并不重要,因为我选择正常或视网膜手动图像。

The images I'm using in the tabbar are dynamic and I load them off the internet. I detect if I'm on retina display or not and download the correct images and initialize the tabbar using them.

Normal images are working fine. My problem is with retina images. They are displayed in big size, bigger than the tabbar, so they are not completely shown.

I wonder how should I load retina images into the tabbar and make them look normal.

Update:

Here's the code I'm using to load the images to the tabbar from each view controller added:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithString:@"1.png"]];
    UIImage* image = [UIImage imageWithContentsOfFile:path];

It doesn't matter if the images have @2x or not as I'm choosing normal or retina images manually.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

画中仙 2024-12-26 19:09:20

我自己没有遇到过这个问题,但我会在创建图像后立即设置一个断点。使用调试器检查新图像的 scale 属性。我想您会发现 scale 设置为 1.0,但您的高分辨率图像应将 scale 设置为 2.0。据我所知,有三种方法可以实现这一点:

  • 用“@2x”后缀命名文件;
  • 适当设置图像的“dpi”属性;
  • 使用 -initWithCGImage:scale:orientation:+imageWithCGImage:scale:orientation: 创建图像

I haven't run across this problem myself, but I'd start by setting a breakpoint just after you create the image. Use the debugger to check the new image's scale property. I think you'll find that scale is set to 1.0, but your high resolution images should have scale set to 2.0. There are three ways (that I know of) to make that happen:

  • Name the file with a "@2x" suffix;
  • Set the 'dpi' attribute for your image appropriately;
  • Create the image with -initWithCGImage:scale:orientation: or +imageWithCGImage:scale:orientation:
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文