【问题标题】:New image name for iPhone 5iPhone 5 的新图像名称
【发布时间】:2012-09-19 15:21:26
【问题描述】:

我们使用retina 制作名称中带有@2x 的图像。我看到默认图像必须是 default-568h@2x 但其他图像似乎并非如此。就像我的背景是 bg.png 和 bg@2x.png 我尝试使用 bg-568h@2x.png 但这不起作用。谁能告诉我这些图片需要命名以支持 iPhone 5 吗?

【问题讨论】:

标签: iphone xcode ios6


【解决方案1】:

iPhone 5(4'' 显示器)没有特殊的后缀,只有特定的 Default-568h@2x.png 文件。

这里有一个宏来处理它:

// iPhone 5 support
#define ASSET_BY_SCREEN_HEIGHT(regular, longScreen) (([[UIScreen mainScreen] bounds].size.height <= 480.0) ? regular : longScreen)

用法:(资产名称 - image.png、image@2x.png、image-568h@2x.png)

myImage = [UIImage imageNamed:ASSET_BY_SCREEN_HEIGHT(@"image",@"image-568h")];

【讨论】:

  • 很好的解决方案!!我建议对宏进行一点改动以简化编码:#define ASSET_BY_SCREEN_HEIGHT(regular) (([[UIScreen mainScreen] bounds].size.height &lt;= 480.0) ? regular : [regular stringByAppendingString:@"-568h"])
  • 非常干净的解决方案! Apple 应该使用 imageNamed: API 自动提供此功能
  • @user1470914 两者都可以,我已将其添加到“macros.h”文件中并将其导入 .pch,因此所有文件都可以使用宏
  • 在 iOS 8 中不再正常工作,因为如果设备处于横向,UIScreen mainScreen 边界会旋转。如果您的应用不旋转,那应该没问题
【解决方案2】:

没有具体的图片名称。拥有 Default-568h@2x 将在 iPhone 5 或 iPod Touch 5G 上启动该图像,并将启用非信箱模式。之后,您需要设计灵活的视图。新尺寸没有特殊的“图像名称”或任何东西。

例如,对于您的背景,您可能应该使用能够拉伸或平铺的图像,并在设置之前对其进行正确配置。

【讨论】:

    【解决方案3】:

    iPhone 5 没有不同的像素密度,它与 iPhone 4/4S 的视网膜显示 PPI 相同,只是屏幕尺寸不同。 @2x 图片将用于 iPhone 5 和 4/4S。

    【讨论】:

    • 这是不同的分辨率。不是 640x960(即 iPhone 4/4S),而是 640x1136。
    • 是的,我的意思是像素密度。
    • 为了保持一致,Wasim,不要将像素数量称为像素密度,这不是一回事。
    【解决方案4】:

    为了完成 Jason 的回答,我建议:如何覆盖 UIImageimageNamed: 方法以使其图像名称的“-568”后缀发生?或者添加一个名为 resolutionAdaptedImageNamed: 的新方法到 UIImage 可能使用一个类别。

    如果我在接下来的几天里有一点时间,我会尝试发布代码。

    注意:不适用于 Nib 文件中的图像。

    【讨论】:

      【解决方案5】:

      如果您使用的是 Xcode 5,则可以使用资产目录(参见那里的用法Apple's documentation

      创建资产目录后,[ UIImage imagedNamed: @"your_image_set" ] 将根据设备提取正确的图像。

      【讨论】:

        【解决方案6】:

        您也可以为此创建类别,如下所示。

        UIImage+Retina4.h
        #import <UIKit/UIKit.h>
        #import <objc/runtime.h>
         @interface UIImage (Retina4)
         @end
        
        UIImage+Retina4.m
        #import "UIImage+Retina4.h"
        static Method origImageNamedMethod = nil;
        @implementation UIImage (Retina4)
        
        + (void)initialize {
        origImageNamedMethod = class_getClassMethod(self, @selector(imageNamed:));
        method_exchangeImplementations(origImageNamedMethod,
                                       class_getClassMethod(self, @selector(retina4ImageNamed:)));
        }
        + (UIImage *)retina4ImageNamed:(NSString *)imageName {
        // NSLog(@"Loading image named => %@", imageName);
        NSMutableString *imageNameMutable = [imageName mutableCopy];
        NSRange retinaAtSymbol = [imageName rangeOfString:@"@"];
        if (retinaAtSymbol.location != NSNotFound) {
            [imageNameMutable insertString:@"-568h" atIndex:retinaAtSymbol.location];
        } else {
            CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
            if ([UIScreen mainScreen].scale == 2.f && screenHeight == 568.0f) {
                NSRange dot = [imageName rangeOfString:@"."];
                if (dot.location != NSNotFound) {
                    [imageNameMutable insertString:@"-568h@2x" atIndex:dot.location];
                } else {
                    [imageNameMutable appendString:@"-568h@2x"];
                }
            }
        }
        
        NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageNameMutable ofType:@"png"];
        if (imagePath) {
            return [UIImage retina4ImageNamed:imageNameMutable];
        } else {
            return [UIImage retina4ImageNamed:imageName];
        }
        return nil;
        }
        
        @end
        

        您可以直接使用导入此类别进行检查,如下所示,您不会检查568或普通图像

        imgvBackground.image=[UIImage imageNamed:@"bkground_bg"];//image name without extantion
        

        【讨论】:

          猜你喜欢
          • 2023-04-03
          • 1970-01-01
          • 2014-11-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-13
          相关资源
          最近更新 更多