preface
Cocoapods
In thePod
There are many ways to cite resources, and different ways to use resources are different, but there are also certain rules. Here I use a sample project to illustrate, the sample project is named:AssetsDemo
, the use ofpod lib create AssetsDemo
Command create, directory structure as shown in figure:thispod
Very simple,Classes
There’s only one directorycell
theswift
andxib
File,Assets
The directory has some image resources, of whichTestTableViewCell.xib
I used this onetest
The image. The directory structure is as follows:
├── Assets │ ├── images.├ ── download.txt │ ├── download.txt │ ├── [email protected] │ │ └ ─ ─ [email protected] │ ├ ─ ─ [email protected] │ └ ─ ─ [email protected] └ ─ ─ Classes ├ ─ ─ TestTableViewCell. Swift └ ─ ─ TestTableViewCell.xibCopy the code
The preliminary work has been prepared. Here are the differences between xiB and image resources in different ways:
willxib
In thesource_files
, pictures inresource_bundles
Podspec is written as follows:
s.source_files = 'AssetsDemo/Classes/**/*'
s.resource_bundles = {
'AssetsDemo'= > ['AssetsDemo/{Assets,Classes}/**/*.{xcassets,png}']}Copy the code
When I run it, I get something like thisframework
:And we can see,xib
The suffix will becomenib
And be put intoframework
, and the image resource is put inAssetsDemo.bundle
In the.
With such a structure, it is obvious that the XIB will not be able to find the image and display it under normal circumstances. However, there is a flawed way to solve this problem, which is to package the PNG image directly into assetsDemo.bundle and change the image name to assetsDemo.bundle/image name in the XIB. This can cause xiB edits to fail to display images properly, the Xcassets directory to store images, and some system optimizations to fail to take effect. This is not recommended
willxib
andThe picture
All in theresources
Podspec is written as follows:
s.source_files = 'AssetsDemo/Classes/**/*.swift'
s.resources = 'AssetsDemo/{Assets,Classes}/**/*.{xcassets,png,xib}'
Copy the code
The specified resources will simply be copied to the target project (main project). The use of resources will be simplified, but there will be conflicts with the main project resource file with the same name. However, this is not the case under Xcode13 and POD 1.11.2. Podspec Syntax Reference v1.11.2 is not the official documentation for this, it is a static library and a dynamic library scenario, static libraries have this problem.
Running the above configuration produces something like thisframework
Structure:
As you can see from the figure above, the resources are directly put into the framework, in addition to the main project package file, there are no such resources. Resources specified in the dynamic library are copied directly into the framework, and do not conflict with the main project resource file.
In addition, because the xib file testTableViewCell. nib and assets. car reside in the same directory, XiB can directly find the image resources from assets. car and display them correctly
- Open this in the main project
xib
File, if the main project has a resource of the same name, then the main project resources will be shown first when editing, but the actual runtime is still thispod
The resources.- here
xib
Can fit into asource_files
andresources
The effect is the same
willxib
andThe picture
All in theresource_bundles
Podspec is written as follows:
s.source_files = 'AssetsDemo/Classes/**/*.swift'
s.resource_bundles = {
'AssetsDemo'= > ['AssetsDemo/{Assets,Classes}/**/*.{xcassets,png,xib}']}Copy the code
Running the above configuration produces something like thisframework
Structure:
The xib file testTableViewCell. nib is in the same directory as assets. car. Xib can find the image resources from assets. car and display them correctly. The Test1 images are not introduced as Xcassets, but they can be found directly and displayed correctly.
conclusion
As a rule, try to keep xiB and Xcassets in the same directory as the images so that xiB can find the images and display them properly.
As for resources and resource_bundles, under the dynamic library, they end up doing the same thing, except that resource_bundles encapsulate resources in bundles and are slightly more cumbersome to use:
// Use resources:
let myBundle = Bundle(for: TestTableViewCell.self)
tableView.register(UINib.init(nibName: "TestTableViewCell", bundle: myBundle),
forCellReuseIdentifier: "cell")
// Using resource_bundles:
let myBundle = Bundle(for: TestTableViewCell.self)
let path = myBundle.path(forResource: "AssetsDemo", ofType: "bundle")!
let assetsBundle = Bundle.init(path: path)
tableView.register(UINib.init(nibName: "TestTableViewCell", bundle: assetsBundle),
forCellReuseIdentifier: "cell")
Copy the code
For static libraries, using resource_bundles is recommended. In general, it is recommended to use resource_bundles to introduce all resources in both static and dynamic libraries, because resources are used in different ways in static and dynamic library scenarios, while resource_bundles are consistent in all scenarios.
As for source_files, it can only import code and XIB files, not other resources such as images. It is not recommended to import XIBs using source_files. If you must use xiBs, be careful to avoid the problem that images in XIBs cannot be loaded.
The resources
- Podspec Syntax Reference v1.11.2
- The resource reference for the Pod library is resource_bundles or resources
By: Starry sky