我想知道一个类似的用例,根据设备类型自动加载 568 像素高的图像。由于没有提供该功能,我想出了一个补丁UIImage
,在 GitHub 上有一个示例项目:
+ (void) load
{
if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone) {
// Running on iPad, nothing to change.
return;
}
CGRect screenBounds = [[UIScreen mainScreen] bounds];
BOOL tallDevice = (screenBounds.size.height > 480);
if (!tallDevice) {
// Running on a 320✕480 device, nothing to change.
return;
}
method_exchangeImplementations(
class_getClassMethod(self, @selector(imageNamed:)),
class_getClassMethod(self, @selector(imageNamedH568:))
);
}
// Note that calling +imageNamedH568: here is not a recursive call,
// since we have exchanged the method implementations for +imageNamed:
// and +imageNamedH568: above.
+ (UIImage*) imageNamedH568: (NSString*) imageName
{
NSString *tallImageName = [imageName stringByAppendingString:@"-568h@2x"];
NSString *tallImagePath = [[NSBundle mainBundle] pathForResource:tallImageName ofType:@"png"];
if (tallImagePath != nil) {
// Tall image found, let’s use it. We just have to pass the
// image name without the @2x suffix to get the correct scale.
imageName = [imageName stringByAppendingString:@"-568h"];
}
return [UIImage imageNamedH568:imageName];
}
您可以使用相同的技巧根据一些自定义名称标签自动加载 iOS 7 资源。同样的警告也适用:该UIImage
技巧使用方法混合,并且可能在生产中具有太多的魔力。你的来电。