如何指定大小为iPhone 6自定义边到边的图像? [英] How to specify size for iPhone 6 customised edge-to-edge image?
问题描述
假设我想要一个捆绑的图片占用iPhone应用程序中所有可用的屏幕宽度,例如横幅。我将创建 my_banner.png
,宽度 320px
, my_banner@2x.png < iPhone 6 plus 的宽度
,宽度
640px
和 my_banner@3x.png
> with width 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.
推荐方式是什么或好方法指定已针对 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自定义边到边的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!