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


//静态库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的工程配置一下


NSBundle (常用API+读取本地资源文件)_第1张图片


//*概况:当前App所包含的库和资源目录,代码只能对此路径内容读取,不能修改
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *rootPath = [[NSBundle mainBundle] resourcePath];
    NSLog(@"App包%@ 包地址%@",bundle,rootPath);
    //App包NSBundle  (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 
    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 *)allBundles;
    //所有Bundle
    NSArray *allBun = [NSBundle allBundles];
    
    
    
    //+ (NSArray *)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];


你可能感兴趣的:(IOS_Foundation库)