如何指定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
像素。仍然与iPhone 4和5共享 @ 2x
后缀,其宽度为 640px
。
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.
指定为<$优化的图像文件的推荐方式或好方法是什么? c $ c> 750px iPhone 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屋!