Bundle是一种特殊的文件夹,主要用于放置工程所要使用的资源。
1.创建Bundle项目
macOS -> Framework & Library -> Bundle
2.将资源拖入项目
3.编译,导出bundle
4.导入使用此Bundle的工程,bundle内部结构如下:
5.使用:
1) UIImage *image = [UIImage imageNamed:@"bundleDemo.bundle/Contents/Resources/jmx.jpg"];
imageView.image= image;
2) NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle].resourcePath
stringByAppendingPathComponent:@"bundleDemo.bundle"]];
NSString*bundlePath = [bundlebundlePath];
NSString*imgPath = [bundlePathstringByAppendingPathComponent:@"Contents/Resources/jmx.jpg"];
UIImage*image = [UIImage imageWithContentsOfFile:imgPath];
imageView.image = image;
其他文件使用类似,需要找到具体路径:
NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle].resourcePath
stringByAppendingPathComponent:@"bundleDemo.bundle"]];
NSString*bundlePath = [bundlebundlePath];
NSString*preferredLanguage = [selfisHan] ?@"zh-Hans":@"en";
NSString *path = [NSString stringWithFormat:@"%@/%@/%@/%@.%@", bundlePath, @"Contents/Resources", @"Localization", preferredLanguage, @"lproj"];
NSBundle*languageBundle = [NSBundlebundleWithPath:path];
NSString*result = [languageBundlelocalizedStringForKey:@"test_bundle"value:@""table:nil];
if (![resultlength]) {
result = [[NSBundle mainBundle] localizedStringForKey:@"test_bundle" value:@"" table:nil];
}
NSLog(@"%@",result);