Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何为iPhone 6/7定制的边到边图像指定尺寸?_Ios_Objective C_Iphone_Cocoa Touch_Iphone 6 - Fatal编程技术网

Ios 如何为iPhone 6/7定制的边到边图像指定尺寸?

Ios 如何为iPhone 6/7定制的边到边图像指定尺寸?,ios,objective-c,iphone,cocoa-touch,iphone-6,Ios,Objective C,Iphone,Cocoa Touch,Iphone 6,假设我想要一个捆绑的图像占据iPhone应用程序中所有可用的屏幕宽度,例如一个横幅。我将创建宽度为320px的my_banner.png,my banner.png_banner@2x.png宽度640px和my_banner@3x.png适用于宽度为1242px的iPhone 6 plus。但是iPhone6的分辨率是750×1334像素。尽管如此,它还是与宽度为640px的iphone4和iphone5共享@2x后缀 指定已针对iPhone 6的750px宽度进行了优化的图像文件的推荐方法是

假设我想要一个捆绑的图像占据iPhone应用程序中所有可用的屏幕宽度,例如一个横幅。我将创建宽度为320px的
my_banner.png
my banner.png_banner@2x.png
宽度
640px
my_banner@3x.png
适用于宽度为1242px的iPhone 6 plus。但是iPhone6的分辨率是750×1334像素。尽管如此,它还是与宽度为640px的iphone4和iphone5共享
@2x
后缀

指定已针对iPhone 6的
750px
宽度进行了优化的图像文件的推荐方法是什么还是好方法?似乎无法在
资产目录中执行此操作
?应该按程序进行吗?iphone6还有其他后缀吗


(图片摘自)

我也找不到办法,因为我有一张背景图片,除了iphone6外,所有设备上的资产目录都与我的背景图片大小完全一致。我的修复(我在SpriteKit中做的)

bgNode是由设备拉起的背景图像。如果是iPhone6,它将不适合屏幕,因此背景图像宽度将与屏幕宽度不同。当设备被识别为iPhone 6时,我将纹理更改为R4纹理(视网膜为@2x),并将其缩放到正确的尺寸


我试着对普通的@2x图像做同样的处理,但是缩放后的图像看起来很糟糕(它太长了,很明显)。随着R4纹理的缩放,宽度/高度的比例稍微好一点,因此变化甚至不明显。我希望这能让您了解在苹果添加iPhone 6资产之前可以做些什么。

我检查了通过Xcode 6从资产目录生成的启动映像的命名约定,以及iPhone 6+的横向版本,例如had:LaunchImage横向-736h@3x.png

基于此,我假设视网膜设备的基本文件沙漠.png:

  • desert@2x:iPhone 4s(320 x 420)
  • 沙漠-568h@2x:iPhone 5、5C和5S(320 x 568)
  • 沙漠-667h@2x:iPhone 6(375 x 667)
  • 沙漠-736h@3x:iPhone 6+(414 x 736)
  • desert@2x~ipad:ipad(1024 x 768)

自动布局应该有助于解决这种情况

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

只是思想的食粮。。自动布局应该照顾您的屏幕,无论您运行应用程序的方向或设备是什么

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

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

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

[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]];

这种情况下没有本机资产支持,因此我认为最好手动操作,因为使用未记录的文件名在将来可能会很容易损坏。

首先,您需要将imageView配置为覆盖所有屏幕,Autolayout将对这项工作有很大帮助,请查看在下面画上墨迹,了解如何使用故事板固定约束(前导空间、尾随空间、顶部空间和底部空间)

第二步是在图像资源(下图)上创建特定于设备的图像集,以根据设备显示不同的图像

查看此信息图表:

它解释了旧式iPhone、iPhone 6和iPhone 6 Plus之间的区别。你可以看到屏幕大小在点、渲染像素和物理像素上的比较

就这些


如果您有任何问题,请提供反馈。

希望这将解决您所有与自定义边到边图像相关的问题。

确保如果使用的是自动布局,则所有边的“check pin”都设置为零,并且“对边距的约束”未选中

您还可以访问此链接获取启动屏幕图像:


只需测量设备尺寸并调用所需的图像即可。即按编程方式进行

所以在你的appdelegate中有全局变量

deviceHeight = self.window.frame.size.height;
deviceWidth = self.window.frame.size.width;
你可以反复打电话。 然后检查它们并调用相应的图像

if (deviceWidth == 640){
image = IPHONE4IMAGE;
deviceString = @"iPhone4";
}
else...

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

UIImage *image = [UIImage imageNamed:@"Default_Image_Name"];
if(IS_IPHONE_^) {
    image = [UIImage imageNamed:@"Iphone6_Image_Name"];
}
“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

在我的例子中,我感兴趣的是使我的基本视图控制器子类具有与启动图像相同的背景图像

注意:除非这是您的具体要求,否则此方法不起作用

此外,即使我尝试创建一个适合iPhone 6(750x1334)的背景图像,将该图像作为图案图像加载到视图的背景色中,最终还是以不希望的方式放大了图像

给了我需要的代码来为我找到一个好的解决方案

下面是我为使我的启动映像与我的
UIViewController
的背景映像相匹配而编写的代码(反之亦然):


我使用了以下技巧,因为有些东西确实有效:

  • 特定设备的资产目录
  • 固相萃取
    #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
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        UIImage *background         = [UIImage imageNamed:[self splashImageName]];
        UIColor *backgroundColor    = [UIColor colorWithPatternImage:background];
        self.view.backgroundColor   = backgroundColor;
    }
    - (NSString *)splashImageName {
        UIInterfaceOrientation orientation  = [[UIApplication sharedApplication] statusBarOrientation];
        CGSize viewSize                     = self.view.bounds.size;
        NSString *viewOrientation           = @"Portrait";
        if (UIDeviceOrientationIsLandscape(orientation)) {
            viewSize                        = CGSizeMake(viewSize.height, viewSize.width);
            viewOrientation                 = @"Landscape";
        }
        NSArray *imagesDict                 = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
        for (NSDictionary *dict in imagesDict) {
            CGSize imageSize                = CGSizeFromString(dict[@"UILaunchImageSize"]);
            if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]])
                return dict[@"UILaunchImageName"];
        }
        return nil;
    }
    
    #define IS_IPHONE_6 [[UIScreen mainScreen]nativeBounds].size.width == 750.0 ? true : false
    
    UIImage *image = [UIImage imageNamed:@"Default_Image_Name"];
    if(IS_IPHONE_^) {
        image = [UIImage imageNamed:@"Iphone6_Image_Name"];
    }
    
    import UIKit
    
    class AGTools: NSObject {
        class func fullWidthImage(imageName: String!) -> String!{
            let screenWidth = UIScreen.mainScreen().bounds.size.width
            switch (screenWidth){
            case 320:
                // scale 2x or 1x
                return (UIScreen.mainScreen().scale > 1.0) ? "\(imageName)@2x" : imageName
            case 375:
                return "\(imageName)-375w@2x"
            case 414:
                return "\(imageName)-414w@3x"
            default:
                return imageName
            }
        }
    }
    
    _imgTest.image = UIImage(named: AGTools.fullWidthImage("imageName"))