NSBundle (常用API+讀取本地資原始檔)
阿新 • • 發佈:2018-12-25
//靜態庫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];