首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

iOS 獲取 App Icon 與 Launch Image 名稱完整指南:Info.plist 解析與適配邏輯

     編輯:Java 相關知識

核心入口:Info.plist 解析

所有 App 圖標與啟動圖資訊均儲存於 `Info.plist`,通過 `[[NSBundle mainBundle] infoDictionary]` 獲取:

  • App Icon:`CFBundleIcons` → `CFBundlePrimaryIcon` → `CFBundleIconFiles` (陣列)
  • Launch Image:`UILaunchImages` (陣列,每項含名稱、尺寸、方向、最低系統版本)

獲取 App Icon 名稱


- (NSString *)primaryIconName {
    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
    NSArray *iconFiles = infoDict[@"CFBundleIcons"][@"CFBundlePrimaryIcon"][@"CFBundleIconFiles"];
    // 最後一個通常為最高解析度(如 AppIcon60x60@3x)
    return iconFiles.lastObject;
}

// 打印所有 Icon 檔名
NSLog(@"Icon files: %@", iconFiles);
// 輸出: ( "AppIcon29x29", "AppIcon40x40", "AppIcon60x60", "AppIcon76x76", "AppIcon83.5x83.5" )

獲取 Launch Image 名稱(含邏輯適配)

方案一:手動匹配裝置尺寸與方向(完全可控)


- (NSString *)launchImageNameForCurrentDevice {
    CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    BOOL isPortrait = UIInterfaceOrientationIsPortrait(orientation);
    BOOL isLandscape = UIInterfaceOrientationIsLandscape(orientation);
    
    NSString *name = nil;
    if ((isPortrait && screenH == 480) || (isLandscape && screenW == 480)) {
        name = @"LaunchImage-700"; // iPhone 4/4S
    } else if ((isPortrait && screenH == 568) || (isLandscape && screenW == 568)) {
        name = @"LaunchImage-700-568h"; // iPhone 5/5s/SE1
    } else if ((isPortrait && screenH == 667) || (isLandscape && screenW == 667)) {
        name = @"LaunchImage-800-667h"; // iPhone 6/6s/7/8/SE2/SE3
    } else if (isPortrait && screenH == 736) {
        name = @"LaunchImage-800-Portrait-736h"; // 6/7/8 Plus 豎屏
    } else if (isLandscape && screenW == 736) {
        name = @"LaunchImage-800-Landscape-736h"; // Plus 橫屏
    } else if (isPortrait && screenH == 1024) {
        name = @"LaunchImage-700-Portrait"; // iPad 豎屏
    } else if (isLandscape && screenW == 1024) {
        name = @"LaunchImage-700-Landscape"; // iPad 橫屏
    }
    // 刘海屏、iPad Pro 等需補充對應尺寸
    return name;
}

// 用法:設置為背景圖
UIImage *img = [UIImage imageNamed:[self launchImageNameForCurrentDevice]];
self.view.backgroundColor = [UIColor colorWithPatternImage:img];

方案二:動態解析 UILaunchImages(自適應配置變更)


- (NSString *)launchImageNameFromPlist {
    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
    NSArray *launchImages = infoDict[@"UILaunchImages"];
    
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    NSString *orientationStr = UIInterfaceOrientationIsPortrait(orientation) ? @"Portrait" : @"Landscape";
    
    for (NSDictionary *imgInfo in launchImages) {
        CGSize size = CGSizeFromString(imgInfo[@"UILaunchImageSize"]);
        NSString *imgOrientation = imgInfo[@"UILaunchImageOrientation"];
        // 匹配尺寸與方向(寬高需考慮縮放比例)
        if (CGSizeEqualToSize(size, screenSize) && [imgOrientation isEqualToString:orientationStr]) {
            return imgInfo[@"UILaunchImageName"];
        }
    }
    return nil;
}

UILaunchImages 結構示例


UILaunchImages = (
    {
        UILaunchImageMinimumOSVersion = "8.0";
        UILaunchImageName = "LaunchImage-800-Portrait-736h";
        UILaunchImageOrientation = Portrait;
        UILaunchImageSize = "{414, 736}";
    },
    {
        UILaunchImageMinimumOSVersion = "7.0";
        UILaunchImageName = "LaunchImage-700-Portrait";
        UILaunchImageOrientation = Portrait;
        UILaunchImageSize = "{768, 1024}";
    },
    ...
);

完整 Info.plist 關鍵鍵值速查

Key含義示例值
CFBundleIconsApp Icon 定義含 CFBundlePrimaryIcon → CFBundleIconFiles
UILaunchImages靜態啟動圖陣列每項含 Name/Size/Orientation/MinOS
UILaunchStoryboardName啟動畫面 StoryboardLaunchScreen (iOS 8+ 推薦)
UIDeviceFamily支援裝置類型1=iPhone, 2=iPad
UISupportedInterfaceOrientations支援旋轉方向Portrait, LandscapeLeft, LandscapeRight
CFBundleShortVersionString用戶可見版本號1.2.3
CFBundleVersion構建版本號100

現代最佳實踐:LaunchScreen.storyboard

iOS 8+ 強烈建議使用 LaunchScreen.storyboard 替代靜態圖片集:

  • 單一檔案適配所有裝置、方向、分辨率、深色模式
  • 支援 Auto Layout、安全區域、動態字體
  • 編譯時優化,無需維護數十張圖片
  • 避免 App 包體積膨脹

/* LaunchScreen.storyboard 典型結構 */
- View Controller
  - View (背景色/圖片)
    - Image View (Logo, 居中約束)
    - Label (版本號, 底部約束)

常見場景應用

場景解決方案
前台收到推送,模擬通知欄顯示 Icon用 `primaryIconName` 獲取圖標名,`[UIImage imageNamed:]` 載入
啟動頁廣告頁面,需無縫銜接 Launch Image解析 `UILaunchImages` 獲取當前裝置對應圖片名,設置為 `UIImageView` 圖片
動態更換 App Icon (iOS 10.3+)`[UIApplication sharedApplication] setAlternateIconName:@"AltIcon"]`,圖標需預先在 `CFBundleAlternateIcons` 配置
適配 iPad 分屏多任務LaunchScreen.storyboard 自動適配;靜態圖需提供所有尺寸組合

避坑指南

  1. ❌ 硬編碼圖片名稱 → 裝置適配失敗、維護地獄
  2. ❌ 忽略 `@2x/@3x` 後綴 → 系統自動追加,代碼中只需基礎名稱
  3. ❌ 混淆 `UILaunchImageSize` (points) 與像素尺寸 → 需乘以 `[UIScreen mainScreen].scale`
  4. ✅ 優先使用 LaunchScreen.storyboard
  5. ✅ 動態解析 Info.plist 而非寫死邏輯
  6. ✅ 測試覆蓋:iPhone SE/Plus/刘海屏、iPad/Pro、豎橫屏、分屏
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved