创建Bundle

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);


demo 链接:https://github.com/licl19/Bundle.git

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容