iOS创建CocoaPods本地私有库

1、第一步先cd进入要创建私有库的目录,然后输入如下命令创建私有库:

// ChatFramework是要创建的私有库的名字
pod lib create ChatFramework

然后按照提示回答几个问题即可:
这里写图片描述

2、命令执行完成后会自动创建并打开一个叫ChatFramework的工程,文件结构如下:

这里写图片描述

先简单介绍下:
ChatFramework.podspec:文件是私有库的配置文件
ChatFramework:该文件夹是存放私有库的类和资源的地方
Example:是根据上图提示,我们选择创建的demo工程,如果选择No,则不会生成此工程

3、现在该是写代码测试私有库的时候啦

3.1 调用私有库的代码

这里写图片描述
在私有库里创建一个Person类,增加一个sayHello方法,然后执行命令pod install重新安装一下私有库。

这里写图片描述

在Example工程里,先导入头文件,再创建一个Person类,可以看到控制台Hello, world!成功打印。

3.2 调用私有库的图片资源

在调用私有库的图片的时候如果使用[UIImage imageNamed:@”login_logo_image”];这种方式去获取图片是拿不到的。因为这种方式是从mainBundle里面找,然鹅,私有库的图片并没有被拷贝到mainBundle里。

加载图片的正确姿势如下:
1.先把图片等资源打包成bundle
这里写图片描述

2.写一个分类,用来加载自己的bundle

#import "NSBundle+Library.h"
#import "CustomView.h"

@implementation NSBundle (Library)

+ (NSBundle *)myLibraryBundle {
    return [self bundleWithURL:[self myLibraryBundleURL]];
}


+ (NSURL *)myLibraryBundleURL {
    NSBundle *bundle = [NSBundle bundleForClass:[CustomView class]];
    return [bundle URLForResource:@"ChatFramework" withExtension:@"bundle"];
}

@end

再写一个分类,用来加载bundle里面的图片

#import "UIImage+Library.h"
#import "NSBundle+Library.h"

@implementation UIImage (Library)

+ (UIImage *)bundleImageNamed:(NSString *)name {
    return [self imageNamed:name inBundle:[NSBundle myLibraryBundle]];
}

+ (UIImage *)imageNamed:(NSString *)name inBundle:(NSBundle *)bundle {
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0
    return [UIImage imageNamed:name inBundle:bundle compatibleWithTraitCollection:nil];
#elif __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0
    return [UIImage imageWithContentsOfFile:[bundle pathForResource:name ofType:nil]];
#else
    if ([UIImage respondsToSelector:@selector(imageNamed:inBundle:compatibleWithTraitCollection:)]) {
        return [UIImage imageNamed:name inBundle:bundle compatibleWithTraitCollection:nil];
    } else {
        return [UIImage imageWithContentsOfFile:[bundle pathForResource:name ofType:nil]];
    }
#endif
}

@end

最后在测试类里面写一个暴露图片的方法,供外界调用

#import "CustomView.h"
#import "UIImage+Library.h"

@implementation CustomView

+ (UIImage *)logoImage
{
    return [UIImage bundleImageNamed:@"login_logo_image"];
}

@end

准备工作做好后,还有一个最重要的步骤:修改podspec配置文件

这里写图片描述

以上步骤完成后,在Example中调用一下:

#import "JYViewController.h"
#import <ChatFramework/CustomView.h>

@interface JYViewController ()

@end

@implementation JYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


//    Person *p = [[Person alloc] init];
//    [p sayHello];

    // 加载图片
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[CustomView logoImage]];
    imageView.frame = CGRectMake(100, 100, 100, 100);
    [self.view addSubview:imageView];
}

@end

Ok,到此结束!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值