如何指定iPhone 6/7自定义边对边图像的大小?

Nik*_*und 60 iphone cocoa-touch objective-c ios iphone-6

假设我想要一个捆绑的图像占用iPhone应用程序中所有可用的屏幕宽度 - 例如横幅.我想创建my_banner.png与宽度320px,my_banner@2x.png与宽度640pxmy_banner@3x.pngiPhone 6加上与宽度1242px.但iPhone 6的分辨率是750×1334像素.仍然@2x与iPhone 4和5 共享后缀,具有640px宽度.

什么是推荐的方式或一个很好的方式来指定已经为优化的图像文件750px的宽度iPhone 6?好像它不能在一个asset catalog?它应该以编程方式完成吗?是否有其他后缀可用于iPhone 6

iPhone 4,5,6屏幕尺寸 (图片摘自http://www.iphoneresolution.com)

Jef*_*Jef 43

在我看来,很多这些答案想要解决如何约束imageView,我认为你担心加载正确的媒体文件?我想出了我自己未来的可扩展解决方案,如下所示:

"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
Run Code Online (Sandbox Code Playgroud)

执行:

#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
Run Code Online (Sandbox Code Playgroud)