如何在运行时获取CFBundleTypeIconFiles信息?

时间:2011-06-28 12:26:39

标签: ios uidocumentinteraction

我有一个适用于pdf文件的应用。我在Info.plist文件中添加了这个:

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeIconFiles</key>
        <array>
            <string>iconPDF</string>
        </array>
        <key>CFBundleTypeName</key>
        <string>PDF</string>
        <key>CFBundleTypeRole</key>
        <string>Viewer</string>
        <key>LSHandlerRank</key>
        <string>Alternate</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>com.adobe.pdf</string>
        </array>
    </dict>
</array>

在我的应用中,我有一个UITableViewController,在(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中,我希望cell.imageView.image设置CFBundleTypeIconFiles中的图标,但我不知道如何获取此图标。
我尝试使用docInteractionController.icons,但在这个数组中我只找到了默认图标。

1 个答案:

答案 0 :(得分:0)

查看HockeyKit中的代码,我在这里完成了以下操作: https://github.com/TheRealKerni/HockeyKit/blob/develop/client/iOS/BWHockeyViewController.m

从第336行开始:

NSString *iconString = nil;
    NSArray *icons = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIconFiles"];
    if (!icons) {
        iconString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIconFile"];
        if (!iconString) {
            iconString = @"Icon.png";
        }
    } else {
        BOOL useHighResIcon = NO;
        IF_IOS4_OR_GREATER(if ([UIScreen mainScreen].scale == 2.0f) useHighResIcon = YES;)

        for(NSString *icon in icons) {
            iconString = icon;
            UIImage *iconImage = [UIImage imageNamed:icon];

            if (iconImage.size.height == 57 && !useHighResIcon) {
                // found!
                break;
            }
            if (iconImage.size.height == 114 && useHighResIcon) {
                // found!
                break;
            }
        }
    }