从其他文件包加载数据

问题:

  在main bundle中可能还会有单独的bundle,这些单独的bundle中也会包含图片或者其它的一些资源,那么如何访问其中的资源呢? 

方案:

  获取除了主文件包之外其他文件包内的文件时最好使用 NSBundle 中 pathForResource:ofType:inDirectory:的实例方法,特别是要直接获取那些制定文件包已经存在的文件和资源。 

 

 NSString *resourceBundlePath = [[NSBundle mainBundle]pathForResource:@"Resources" ofType:@"bundle"];
        if ([resourceBundlePath length] > 0) {
            NSBundle *resourceBundle = [NSBundle bundleWithPath:resourceBundlePath];
            if (resourceBundle != nil) {
                NSString *pathToImage = [resourceBundle pathForResource:@"lixw_20140426_003" ofType:@"jpg" inDirectory:@"Images"];
                if ([pathToImage length] > 0) {
                    NSLog(@"图片位置%@",pathToImage);
                }else{
                    NSLog(@"没有找到");
                }
            }else{
                NSLog(@"加载bundle失败");
            }
        }else{
            NSLog(@"找不到指定的bundle");
        }
如果你尝试查找文件包内特定文件夹里保存的所有资源,可以使用 NSBundle 类的实例 方法 pathsForResourcesOfType:inDirectory: 

 

NSArray *paths = [resourcesBundle pathsForResourcesOfType:@"png"  inDirectory:@"images"];
[paths enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
NSLog(@"Path%lu = %@",(unsigned long)idx+1,obj);
}];

 

posted @ 2014-10-08 15:21  safiri  阅读(178)  评论(0编辑  收藏  举报