假设我想要一个Bundle 的图像占据iPhone应用程序中所有可用的屏幕宽度,例如一个横幅.我会创建宽度为320pxmy_banner.png,宽度为640pxmy_banner@2x.png和宽度为1242pxiPhone 6 plus.但iPhone 6的分辨率是750×1334像素.不过,它与iPhone 4和iPhone 5的后缀相同,后者的宽度为640px.

recommended waygood way是什么来指定一个已针对iPhone 6750px宽度进行了优化的图像文件?似乎不能在asset catalog里完成?应该按程序进行吗?iPhone 6还有其他后缀吗?

iPhone 4,5,6屏幕尺寸

推荐答案

在我看来,这些答案中有很多都想解决如何约束imageView的问题,我认为您关心的是加载正确的媒体文件?我会提出自己future 的可扩展解决方案,比如:

"UIImage+DeviceSpecificMedia.h"-(UIImage上的一个类别)

接口:

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger, thisDeviceClass) {

    thisDeviceClass_iPhone,
    thisDeviceClass_iPhoneRetina,
    thisDeviceClass_iPhone5,
    thisDeviceClass_iPhone6,
    thisDeviceClass_iPhone6plus,

    // we can add new devices when we become aware of them

    thisDeviceClass_iPad,
    thisDeviceClass_iPadRetina,


    thisDeviceClass_unknown
};

thisDeviceClass currentDeviceClass();

@interface UIImage (DeviceSpecificMedia)

+ (instancetype )imageForDeviceWithName:(NSString *)fileName;

@end

实施:

#import "UIImage+DeviceSpecificMedia.h"

thisDeviceClass currentDeviceClass() {

    CGFloat greaterPixelDimension = (CGFloat) fmaxf(((float)[[UIScreen mainScreen]bounds].size.height),
                                                    ((float)[[UIScreen mainScreen]bounds].size.width));

    switch ((NSInteger)greaterPixelDimension) {
        case 480:
            return (( [[UIScreen mainScreen]scale] > 1.0) ? thisDeviceClass_iPhoneRetina : thisDeviceClass_iPhone );
            break;
        case 568:
            return thisDeviceClass_iPhone5;
            break;
        case 667:
            return thisDeviceClass_iPhone6;
            break;
        case 736:
            return thisDeviceClass_iPhone6plus;
            break;
        case 1024:
            return (( [[UIScreen mainScreen]scale] > 1.0) ? thisDeviceClass_iPadRetina : thisDeviceClass_iPad );
            break;
        default:
            return thisDeviceClass_unknown;
            break;
    }
}

@implementation UIImage (deviceSpecificMedia)

+ (NSString *)magicSuffixForDevice
{
    switch (currentDeviceClass()) {
        case thisDeviceClass_iPhone:
            return @"";
            break;
        case thisDeviceClass_iPhoneRetina:
            return @"@2x";
            break;
        case thisDeviceClass_iPhone5:
            return @"-568h@2x";
            break;
        case thisDeviceClass_iPhone6:
            return @"-667h@2x"; //or some other arbitrary string..
            break;
        case thisDeviceClass_iPhone6plus:
            return @"-736h@3x";
            break;

        case thisDeviceClass_iPad:
            return @"~ipad";
            break;
        case thisDeviceClass_iPadRetina:
            return @"~ipad@2x";
            break;

        case thisDeviceClass_unknown:
        default:
            return @"";
            break;
    }
}

+ (instancetype )imageForDeviceWithName:(NSString *)fileName
{
    UIImage *result = nil;
    NSString *nameWithSuffix = [fileName stringByAppendingString:[UIImage magicSuffixForDevice]];

    result = [UIImage imageNamed:nameWithSuffix];
    if (!result) {
        result = [UIImage imageNamed:fileName];
    }
    return result;
}

@end

Objective-c相关问答推荐

iPhone 可达性判断

如何使用 UIStepper

UITabBar 中的自定义 colored颜色

具有自定义视图的相机

防止同时多个按钮

在 iOS 中检测 PAN 手势的方向

我应该打电话给 [super awakeFromNib] 吗?

NSTimeZone:localTimeZone 和 systemTimeZone 有什么区别?

为什么当我使其处于非活动状态时,弱 IBOutlet NSLayoutConstraint 变为 nil?

如何在 UICollectionView 中居中行?

你如何安排一个块在下一次运行循环迭代中运行?

Select 时如何更改 UITableViewCell 的 colored颜色 ?

接口类型不能静态分配?

UILabel 的角半径属性在 iOS 7.1 中不起作用

iOS:如何将 UIViewAnimationCurve 转换为 UIViewAnimationOptions?

UITableView configureCellForDisplay:forIndexPath 中的断言失败:

如何用 Java 开发 iPhone 应用程序?

为什么 LLDB 不能打印 view.bounds?

如何以编程方式在 UINavigationController 中按下返回按钮

如何定义 UIColor 的常量值?