比方说,我想要一个捆绑的图像占据iPhone应用程序中所有可用的屏幕宽度--例如横幅。我会创建宽度为320px
的my_banner.png
,宽度为640px
的my_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的后缀
发布于 2014-11-01 06:48:37
在我看来,这些答案中的许多都想解决如何约束imageView,我认为您关心的是加载正确的媒体文件?我会想出我自己未来的可扩展解决方案,就像这样:
"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
实施:
#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
发布于 2015-04-22 14:29:08
我正在使用以下技巧,因为一些东西实际上是有效的:
针对特定devices
1x
、2x
基于4 2x
的320x640
3x
(iPhone 5)
声明一个常量
#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"];
}
这可能不是最好的解决方案,但它是有效的,至少只要苹果没有为边缘到边缘绑定提供更好的API。
发布于 2014-10-15 22:51:40
自动布局应该可以帮助解决这种情况。
现在告诉我,@Nicklas Berglund,如果设备旋转,你会怎么做?假设您现在处于横向模式..您将如何填充图像资源中不存在的水平空间?
只是思想的食粮..自动布局应该会照顾到你的屏幕,无论你在哪个方向上,或者你在哪个设备上运行你的应用程序。
也许苹果应该在未来开始在图像资产中定位设备方向?
让我们回到你的问题上..解决方案是用750px宽的图像替换你的@2x图像,然后让Auto Layout完成它的工作。哦,是的,这就是棘手的部分..
如果你只是添加约束来适应它,当它显示在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]];
https://stackoverflow.com/questions/25892207
复制相似问题