Tim*_*imm 41 xcode objective-c ios ios7
有没有办法使用应用程序LaunchImage作为通用iOS应用程序的背景,而无需在多个地方放置相同的图像资源?
我无法访问LaunchImage文件Images.xcassets,因此我创建了两个新的图像集"背景肖像"和"背景风景"(因为似乎没有办法将横向和纵向图像放入同一组).
虽然这个解决方法完成了这些工作,但我不愿意将每个图像都包含在应用程序中两次.这也具有高维护成本.
任何有关如何访问当前设备的LaunchImage的建议都表示赞赏.
GCOLaunchImageTransition必须完成iOS <7的工作.
Joh*_*rck 56
您可以复制/粘贴以下代码以在运行时加载应用程序的启动映像:
// Load launch image
NSString *launchImageName;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
if ([UIScreen mainScreen].bounds.size.height == 480) launchImageName = @"LaunchImage-700@2x.png"; // iPhone 4/4s, 3.5 inch screen
if ([UIScreen mainScreen].bounds.size.height == 568) launchImageName = @"LaunchImage-700-568h@2x.png"; // iPhone 5/5s, 4.0 inch screen
if ([UIScreen mainScreen].bounds.size.height == 667) launchImageName = @"LaunchImage-800-667h@2x.png"; // iPhone 6, 4.7 inch screen
if ([UIScreen mainScreen].bounds.size.height == 736) launchImageName = @"LaunchImage-800-Portrait-736h@3x.png"; // iPhone 6+, 5.5 inch screen
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
if ([UIScreen mainScreen].scale == 1) launchImageName = @"LaunchImage-700-Portrait~ipad.png"; // iPad 2
if ([UIScreen mainScreen].scale == 2) launchImageName = @"LaunchImage-700-Portrait@2x~ipad.png"; // Retina iPads
}
self.launchImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:launchImageName]];
Run Code Online (Sandbox Code Playgroud)
Mat*_*rke 42
您可以使用启动图像,而无需包含它们两次.关键是当您使用资产目录时,应用程序包中包含的图像的文件名是(有点)标准化的,可能与您为原始文件命名的内容无关.
特别是,当您使用LaunchImage映像集时,最终在应用程序包中的文件具有类似的名称
注意,特别是,它们没有命名Default.png或任何变体.即使那就是你所谓的文件.一旦将它们放入资产目录中的一个井中,它们就会以标准名称出现在另一端.
所以[UIImage imageNamed:@"Default"]不起作用,因为应用程序包中没有这样的文件.但是,[UIImage imageNamed:@"LaunchImage"]可以工作(假设您已经填充了iPhone Portrait 2x或者iOS7 iPhone 前 1x井).
该文档表明该imageNamed:方法UIImage应该自动选择正确的版本,但我认为这仅适用于启动图像以外的图像集 - 至少我没有让它正常工作(可能只是我没有做正确的事).
因此,根据您的具体情况,您可能需要进行一些试验和错误才能获得正确的文件名.在模拟器中构建并运行应用程序,然后您可以始终查看相应的子目录,~/Library/Application Support/iPhone Simulator以验证应用程序包中的实际文件名是什么.
但同样,主要的一点是,不需要包含图像文件的副本,也不需要对Copy Bundle Resources构建阶段进行任何调整.
Dan*_*rna 41
大多数答案需要根据设备类型,比例,大小等创建图像名称.但正如Matthew Burke指出的那样,启动图像目录中的每个图像都将重命名为"LaunchImage*",因此我们能够遍历启动图像并找到(对于当前设备)适当的图像.在Objective-C中,它看起来像这样:
NSArray *allPngImageNames = [[NSBundle mainBundle] pathsForResourcesOfType:@"png"
inDirectory:nil];
for (NSString *imgName in allPngImageNames){
// Find launch images
if ([imgName containsString:@"LaunchImage"]){
UIImage *img = [UIImage imageNamed:imgName];
// Has image same scale and dimensions as our current device's screen?
if (img.scale == [UIScreen mainScreen].scale && CGSizeEqualToSize(img.size, [UIScreen mainScreen].bounds.size)) {
NSLog(@"Found launch image for current device %@", img.description);
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
(请注意,此代码使用iOS 8中引入的"containsString"方法.对于以前的iOS版本,使用"rangeOfString")
ZYi*_*iOS 10
以下是我在iOS 7.0+中测试时的结果,只有肖像画面:
3.5 inch screen: LaunchImage-700@2x.png
4.0 inch screen: LaunchImage-700-568h@2x.png
4.7 inch screen: LaunchImage-800-667h@2x.png
5.5 inch screen: LaunchImage-800-Portrait-736h@3x.png
iPad2 : LaunchImage-700-Portrait~ipad.png
Retina iPads : LaunchImage-700-Portrait@2x~ipad.png
Run Code Online (Sandbox Code Playgroud)
Daniel Witurna 的优秀答案的Swift版本,不需要检查所有已知设备类型或方向的列表.
func appLaunchImage() -> UIImage?
{
let allPngImageNames = NSBundle.mainBundle().pathsForResourcesOfType("png", inDirectory: nil)
for imageName in allPngImageNames
{
guard imageName.containsString("LaunchImage") else { continue }
guard let image = UIImage(named: imageName) else { continue }
// if the image has the same scale AND dimensions as the current device's screen...
if (image.scale == UIScreen.mainScreen().scale) && (CGSizeEqualToSize(image.size, UIScreen.mainScreen().bounds.size))
{
return image
}
}
return nil
}
Run Code Online (Sandbox Code Playgroud)
Info.plist 捆绑包中的包含启动映像信息,包括启动映像的名称。
目标C:
- (UIImage *)getCurrentLaunchImage {
CGSize screenSize = [UIScreen mainScreen].bounds.size;
NSString *interfaceOrientation = nil;
if (([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown) ||
([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait)) {
interfaceOrientation = @"Portrait";
} else {
interfaceOrientation = @"Landscape";
}
NSString *launchImageName = nil;
NSArray *launchImages = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
for (NSDictionary *launchImage in launchImages) {
CGSize launchImageSize = CGSizeFromString(launchImage[@"UILaunchImageSize"]);
NSString *launchImageOrientation = launchImage[@"UILaunchImageOrientation"];
if (CGSizeEqualToSize(launchImageSize, screenSize) &&
[launchImageOrientation isEqualToString:interfaceOrientation]) {
launchImageName = launchImage[@"UILaunchImageName"];
break;
}
}
return [UIImage imageNamed:launchImageName];
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特4:
func getCurrentLaunchImage() -> UIImage? {
guard let launchImages = Bundle.main.infoDictionary?["UILaunchImages"] as? [[String: Any]] else { return nil }
let screenSize = UIScreen.main.bounds.size
var interfaceOrientation: String
switch UIApplication.shared.statusBarOrientation {
case .portrait,
.portraitUpsideDown:
interfaceOrientation = "Portrait"
default:
interfaceOrientation = "Landscape"
}
for launchImage in launchImages {
guard let imageSize = launchImage["UILaunchImageSize"] as? String else { continue }
let launchImageSize = CGSizeFromString(imageSize)
guard let launchImageOrientation = launchImage["UILaunchImageOrientation"] as? String else { continue }
if
launchImageSize.equalTo(screenSize),
launchImageOrientation == interfaceOrientation,
let launchImageName = launchImage["UILaunchImageName"] as? String {
return UIImage(named: launchImageName)
}
}
return nil
}
Run Code Online (Sandbox Code Playgroud)