如何为 iPhone 6/7 自定义边到边图像指定尺寸? [英] How to specify size for iPhone 6/7 customised edge-to-edge image?
问题描述
假设我想要一个捆绑的图像占据 iPhone 应用程序中所有可用的屏幕宽度 - 例如横幅.我会创建 my_banner.png
宽度为 320px
,my_banner@2x.png
宽度为 640px
和 >my_banner@3x.png
用于 iPhone 6 plus 宽度 1242px
.但是iPhone 6的分辨率是750×1334
像素.它仍然与具有 640px
宽度的 iPhone 4 和 5 共享 @2x
后缀.
Say I want a bundled image to take up all available screen width in an iPhone app - for example a banner. I'd create my_banner.png
with width 320px
, my_banner@2x.png
with width 640px
and my_banner@3x.png
for iPhone 6 plus with width 1242px
. But the resolution of iPhone 6 is 750×1334
pixels. Still it shares the @2x
suffix with iPhone 4 and 5 that have 640px
width.
推荐的方法或好的方法是什么来指定针对iPhone的750px
宽度优化的图像文件6?好像不能在资产目录
中完成?它应该以编程方式完成吗?是否还有其他后缀可用于 iPhone 6?
What's the recommended way or a good way to specify an image file that has been optimised for the 750px
width of iPhone 6? Seems like it cannot be done in an asset catalog
? Should it be done programatically? Is there some other suffix that can be used for iPhone 6?
(图片摘自http://www.iphoneresolution.com)
推荐答案
在我看来,很多这些答案都想解决如何约束 imageView,我认为您关心加载正确的媒体文件?我会想出我自己未来的可扩展解决方案,如下所示:
It seems to me that a lot of these answers want to address how to constrain the imageView, where I think you are concerned with loading the correct media file? I would come up with my own future extensible solution, something like this:
"UIImage+DeviceSpecificMedia.h" - (UIImage 上的一个类别)
"UIImage+DeviceSpecificMedia.h" - (a category on 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
这篇关于如何为 iPhone 6/7 自定义边到边图像指定尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!