从其他文件包加载数据

问题:

  在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);
}];

 

转载于:https://www.cnblogs.com/safiri/p/4011293.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值