ios-bundle

iOS 中如何加载资源。

读取第三方Framework中的资源文件

先看看IPA文件目录结构。右键选中文件然后”显示包内容”:

图中可以看到百度地图SDK的 mapapi.bundle 文件。

1
2
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"mapapi" ofType:@"bundle"];
NSLog(@"bundle path is %@", bundlePath);

输出如下日志:

1
2020-11-13 16:10:44.289957+0800 map-sdk-ios-framework[19583:1397775] bundle path  is /private/var/containers/Bundle/Application/57181523-430B-42C0-AFD3-450AC94D14AF/map-sdk-ios-framework.app/mapapi.bundle

百度地图SDK的 mapapi.bundle 文件包含一张名为 baidumap_logo.png 的图片。

我们可以进一步读取这张图片。代码如下:

1
2
3
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"mapapi" ofType:@"bundle"];
NSData *localData = [NSData dataWithContentsOfFile:[bundlePath stringByAppendingPathComponent:@"images/baidumap_logo.png" ]];
NSLog(@"bundle data is %@", localData);

问题记录

无法读取依赖工程中的资源