NSBundle (常用API+读取本地资源文件)

本文详细介绍了如何在Objective-C环境下访问Framework的内部资源,包括如何通过Bundle获取图片、文件等资源,并提供了实例代码演示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


//静态库Framework中访问内部 image、bundle

UIImageView *im = [[UIImageView alloc]initWithFrame:CGRectMake(200, 200, 300, 300)];
        im.backgroundColor = [UIColor blueColor];
        NSLog(@"%@", im);

        
        im.image = [UIImage imageNamed:@"DemoSDK_CheckImage.framework/11.png"];
        
        im.image = [UIImage imageNamed:@"DemoSDK_CheckImage.framework/xxxx.bundle/info.png"];

在调用Framework的工程配置一下




//*概况:当前App所包含的库和资源目录,代码只能对此路径内容读取,不能修改
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *rootPath = [[NSBundle mainBundle] resourcePath];
    NSLog(@"App包%@ 包地址%@",bundle,rootPath);
    //App包NSBundle </private/var/mobile/Containers/Bundle/Application/ED14EC32-AECE-44E1-80A1-5138199137FC/NSBundle.app> (loaded) 包地址/private/var/mobile/Containers/Bundle/Application/ED14EC32-AECE-44E1-80A1-5138199137FC/NSBundle.app
    
    
    
    
    //*后缀为.bundle包资源获取
    //bundle根路径 /private/var/mobile/Containers/Bundle/Application/7988C196-9637-4CB4-B90B-C4E718204888/NSBundle.app/imageBundle.bundle
    NSString *pathStr = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"imageBundle.bundle"];
    //获取整个Bundle </var/mobile/Containers/Bundle/Application/7988C196-9637-4CB4-B90B-C4E718204888/NSBundle.app/imageBundle.bundle>
    NSBundle *pathBundle = [NSBundle bundleWithPath:pathStr];
    //获取Bundle里的资源路径
    NSString *imgPath = [pathBundle pathForResource:@"004" ofType:@"png"];
    //显示资源
    UIImage *imgO = [UIImage imageWithContentsOfFile:imgPath];
    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageWithContentsOfFile:imgPath]];
    image.image = imgO;
    image.frame = CGRectMake(10, 10, 50, 50);
    [self.view addSubview:image];
    
    
    
    //+ (NSBundle *)bundleForClass:(Class)aClass;
//    NSBundle *bundleCla = [NSBundle bundleForClass:bundleClass];

    
    
    //+ (nullable NSBundle *)bundleWithIdentifier:(NSString *)identifier;
    //
    
    
    
    //+ (NSArray<NSBundle *> *)allBundles;
    //所有Bundle
    NSArray *allBun = [NSBundle allBundles];
    
    
    
    //+ (NSArray<NSBundle *> *)allFrameworks;
    //当前App所有库
    NSArray *allFrameW = [NSBundle allFrameworks];
    
    
    
    /* Methods for loading and unloading bundles. */
    //- (BOOL)load; 不需要调用
    BOOL isLoadNow = [bundle load];
    
    
    
    //@property (readonly, getter=isLoaded) BOOL loaded;
    //不需要调用
    BOOL isLoad = bundle.loaded;
    
    
    
    
    //- (BOOL)unload;
    
    
    
    //- (BOOL)preflightAndReturnError:(NSError **)error NS_AVAILABLE(10_5, 2_0);
    //
    NSError *erro;
    BOOL error = [pathBundle preflightAndReturnError:&erro];
    
    
    
    
    //- (BOOL)loadAndReturnError:(NSError **)error NS_AVAILABLE(10_5, 2_0);
    //
    BOOL error2 = [pathBundle loadAndReturnError:&erro];

    
    
    
    //@property (readonly, copy) NSURL *bundleURL NS_AVAILABLE(10_6, 4_0);
    //Bundle 的URL形式
    NSURL *bundleU = [bundle bundleURL];
    
    
    
    //@property (nullable, readonly, copy) NSURL *resourceURL NS_AVAILABLE(10_6, 4_0);
    //
    NSURL *resouU = [bundle resourceURL];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值