1.组件中的类要分文件夹咋办?
如下图s.source_file的值一样要由原来的'HTTool/Classes//'改成'HTLog/Classes/.{h,m}'。要不所有的文件还是都放在一起。
创建文件夹用关键字s.subspec,并制定文件夹的文件路径
![](https://img.haomeiwen.com/i5146735/2ee8d9209a369452.jpeg)
2.组件中一个文件中的类引用了另一个文件中的类,这时候你在组件过程中可以编译通过,到时pod lib lint 却过不去。处理如下图
注意这里用到了关键字dependency,且这里路径中没有了Classes
![](https://img.haomeiwen.com/i5146735/f48e78c075d9cad1.jpeg)
3.加载组件中的图片。
先看图片在组件中的存放位置:
![](https://img.haomeiwen.com/i5146735/760961be4c8668ee.png)
同时修改podspec配置文件
s.resource_bundles = {
'HTUITool' => ['HTUITool/Assets/*']
}
这样就无法加载图片了,原因是这种方式默认是从mainBundle中去加载图片,
然而组件化之后,图片已经不再mainBundle中了,实际是在对应组件下的bundle 里面。
_imgView.image = [UIImage imageNamed:@"user@2x.png"];
有两种方式去加载图片
NSInteger myscale =[UIScreen mainScreen].scale;
NSString *str =[NSString stringWithFormat:@"HTUITool.bundle/user@%zx.png",myscale];
UIImage *image = [UIImage imageNamed:str];
2.拿到图片的绝对路径。这个方法在原有类的分类中没法使用。
NSInteger myscale =[UIScreen mainScreen].scale;
//拿到该类所在的Bundle
NSBundle *currentBundle = [NSBundle bundleForClass:[self class]];
NSString *patch = [currentBundle pathForResource:[NSString stringWithFormat:@"user@%zx.png",myscale] ofType:nil inDirectory:@"HTUITool.bundle"];
UIImage *myimage = [UIImage imageWithContentsOfFile:patch];
方案2 可以封装到一个uiimage的分类里面,在将这个分类放到基础组件。
#import <UIKit/UIKit.h>
@interface UIImage (wgBundle)
+ (instancetype)wg_imgWithName:(NSString *)name bundle:(NSString *)bundleName targetClass:(Class)targetClass;
@end
#import "UIImage+wgBundle.h"
@implementation UIImage (wgBundle)
+ (instancetype)wg_imgWithName:(NSString *)name bundle:(NSString *)bundleName targetClass:(Class)targetClass{
NSInteger scale = [[UIScreen mainScreen] scale];
NSBundle *curB = [NSBundle bundleForClass:targetClass];
NSString *imgName = [NSString stringWithFormat:@"%@@%zdx.png", name,scale];
NSString *dir = [NSString stringWithFormat:@"%@.bundle",bundleName];
NSString *path = [curB pathForResource:imgName ofType:nil inDirectory:dir];
return path?[UIImage imageWithContentsOfFile:path]:nil;
}
@end
4.自己的一个组件要依赖自己写的另一个组件,除了在.podspec文件里面写上s.dependency,还有在测试工程的podfile文件里面写上要依赖的组件的索引库的地址
use_frameworks!
source 'https://github.com/CocoaPods/Specs.git'
source 'http://172.16.1.161/app/BYSpecs.git'
platform :ios, '8.0'
target 'BYCommon_Example' do
pod 'BYCommon', :path => '../'
target 'BYCommon_Tests' do
inherit! :search_paths
end
end
此外,pod lib lint验证spec文件时可能会出现如下错误的时候
- ERROR | [iOS] unknown: Encountered an unknown error (Unable to find a specification for `BYUtil (~> 0.1.1)` depended upon by `BYCommon`) during validation.
我们的 podspec 文件在写依赖的时候也无法在对应的库后面添加源地址。但是我们可以在验证和提交的时候多写一个参数。
那就是--sources 参数,给--sources 赋值上要依赖组件的源。
pod lib lint --sources=http://172.16.1.161/app/BYSpecs.git
网友评论