After learning the previous two articles, I believe that we have a general understanding of componentization development, so let’s talk about resource file loading in this article
Here I created a new LXFMain library, which is mainly used to display TabBar widgets, and then componentizing them out. I don’t want to describe the process here, but if you haven’t read about it, I suggest you read these two articles first
IOS componentized development (I) : basic use of remote private libraries
IOS componentized development (2) : update and sub-library of remote private library
What is different here is that there are more image resources. The core code of the component is stored in the Classes folder, while the images are stored in the Assets directory, as shown in the figure
Modify the Spec
Remove comments about resource loading
s.resource_bundles = {
# 'LXFMain' => ['LXFMain/Assets/*.png']
'LXFMain'= > ['LXFMain/Assets/*']}Copy the code
Back in LXFMain’s template library, we do a local pod install.
As you can see, the image resource is also installed, but the running effect is as shown below. The image does not load successfully
2. Modify the load resource code
This is the relevant code for the currently loaded image
[UIImage imageNamed:@"Picture Name"];
Copy the code
Right-click to display the package contents
The lxFmain. bundle contains the following images (I won’t take a screenshot here), but this is to give you an idea of the directory structure
We jump to define for imageNamed
// load from main bundle
Copy the code
As you can see, imageNamed is officially commented to load resources in the mainBundle. The location of the Main Bundle is shown below
We need to make it load the images in the bundle it is currently in, so the code that loads the images needs to be modified
NSString *normalImgName = @ "personal @ 2 x. PNG";
NSBundle *curBundle = [NSBundle bundleForClass:self.class]; // Get the current bundle
NSString *normalImgPath = [curBundle pathForResource:normalImgName ofType:nil inDirectory:@"LXFMain.bundle"];
UIImage *normalImage = [UIImage imageWithContentsOfFile:normalImgPath];
Copy the code
But writing lxfmain. bundle directly is not good, it is not controllable, so it needs to be improved:
NSString *normalImgName = [NSString stringWithFormat:@"%@@2x.png", normalImg]; NSBundle *curBundle = [NSBundle bundleForClass:self.class]; / / * * * * * * * * * * * on * * * * * * * * * * * / / nsstrings. * curBundleName = curBundle infoDictionary [@"CFBundleName"];
NSString *curBundleDirectory = [NSString stringWithFormat:@"%@.bundle", curBundleName];
NSString *normalImgPath = [curBundle pathForResource:normalImgName ofType:nil inDirectory:curBundleDirectory];
// *************************** //
UIImage *normalImage = [UIImage imageWithContentsOfFile:normalImgPath];
Copy the code
Talk about XIB
The same is true for Xib loading
NSBundle *curBundle = [NSBundle bundleForClass:self.class];
LXFCenterView *centerView = (LXFCenterView *)[curBundle loadNibNamed:@"LXFCenterView" owner:nil options:nil].firstObject;
centerView.frame = CGRectMake(30, 140, 200, 100);
[self.view addSubview:centerView];
Copy the code
In xiB, however, it is worth mentioning that if we drag an imageView control directly into the XIB to set the image load, we need to add the current bundle name before the image name
LXFMain. Bundle/individualCopy the code
In addition to the reason that the image to be loaded by the XIB does not belong to the mainBundle, another point is that the XIB file and the bundle are stored in the same level, so you can directly use the relative path and add the bundle name before the image name.
You can’t see the effect directly on the XIB, but it does work
Four, encountered small problems
[!] Unable to find a pod with name, author, summary, or description matching `lxfmain`Copy the code
The solution is to delete the local index file, and then search again. The system will automatically regenerate all the local index files for you, and then you are done
rm -rf ~/Library/Caches/CocoaPods/search_index.json
pod search lxfmain
Copy the code