首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何为iPhone 6/7定制的边缘到边缘图像指定大小?

如何为iPhone 6/7定制的边缘到边缘图像指定大小?
EN

Stack Overflow用户
提问于 2014-09-17 21:39:04
回答 12查看 55.2K关注 0票数 60

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

要指定针对iPhone 6的750px宽度进行优化的图像文件,推荐的方式good way是什么?这似乎不能在asset catalog中完成?它应该通过编程来完成吗?是否有其他可用于iPhone 6的后缀

(图片摘自http://www.iphoneresolution.com)

EN

回答 12

Stack Overflow用户

回答已采纳

发布于 2014-11-01 06:48:37

在我看来,这些答案中的许多都想解决如何约束imageView,我认为您关心的是加载正确的媒体文件?我会想出我自己未来的可扩展解决方案,就像这样:

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

接口:

代码语言:javascript
复制
#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

实施:

代码语言:javascript
复制
#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
票数 43
EN

Stack Overflow用户

发布于 2015-04-22 14:29:08

我正在使用以下技巧,因为一些东西实际上是有效的:

针对特定devices

  • Specify图像的

  • 资产目录1x2x基于4 2x的320x640

  • Specify图像和基于320x568的3x (iPhone 5)

  • 专门为iPhone 6创建新的图像集(因为这是唯一不适合边缘到边缘的设备bindings)

  • Only提供全分辨率(750x1334)的iPhone 6图像)<代码>H215<代码>F216

声明一个常量

代码语言:javascript
复制
#define IS_IPHONE_6 [[UIScreen mainScreen]nativeBounds].size.width == 750.0 ? true : false

并像这样使用它:

代码语言:javascript
复制
UIImage *image = [UIImage imageNamed:@"Default_Image_Name"];
if(IS_IPHONE_^) {
    image = [UIImage imageNamed:@"Iphone6_Image_Name"];
}

这可能不是最好的解决方案,但它是有效的,至少只要苹果没有为边缘到边缘绑定提供更好的API。

票数 4
EN

Stack Overflow用户

发布于 2014-10-15 22:51:40

自动布局应该可以帮助解决这种情况。

现在告诉我,@Nicklas Berglund,如果设备旋转,你会怎么做?假设您现在处于横向模式..您将如何填充图像资源中不存在的水平空间?

只是思想的食粮..自动布局应该会照顾到你的屏幕,无论你在哪个方向上,或者你在哪个设备上运行你的应用程序。

也许苹果应该在未来开始在图像资产中定位设备方向?

让我们回到你的问题上..解决方案是用750px宽的图像替换你的@2x图像,然后让Auto Layout完成它的工作。哦,是的,这就是棘手的部分..

如果你只是添加约束来适应它,当它显示在4英寸屏幕上时,它会水平挤压它,但你可以使用乘法器来适当地缩放图像。下面是你如何做到的:

代码语言:javascript
复制
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[imageFooterView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(imageFooterView)]];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[imageFooterView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(imageFooterView)]];


float aspectRatio = imageFooterView.frame.size.height/imageFooterView.frame.size.width;

[imageFooterView addConstraint:[NSLayoutConstraint constraintWithItem:imageFooterView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:imageFooterView attribute:NSLayoutAttributeWidth multiplier:aspectRatio constant:0.0f]];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25892207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档