iOS swift pods 私有库 资源xib,png 加载
2020-11-05 本文已影响0人
秋叶红90
.podspec 文件更改 例子
s.resource_bundles = {
'SHMusicPlayer' => ['SHMusicPlayer/Assets/*']
}
/// 加载xib
public protocol SHNibLoad:UIView,SHResrouceLoad {
static func sh_loadViewFromXib() -> Self?;
}
/// 资源加载类
public protocol SHResrouceLoad:class {
/// 加载图片
/// - Parameter imgName: 图片名称
func sh_loadImg(_ imgName:String) -> UIImage?;
}
extension SHNibLoad{
public static func sh_loadViewFromXib() -> Self?{
let curr = Bundle.sh_bundleFor(aClass: Self.self)
return curr?.loadNibNamed("\(Self.self)", owner: nil, options: nil)?.first as? Self
}
}
extension SHResrouceLoad{
public func sh_loadImg(_ imgName:String) -> UIImage?{
return UIImage.sh_imgName(imgName, aClass: Self.self)
}
}
extension UIImage{
public static func sh_imgName(_ imgName:String,aClass:AnyClass) -> Self? {
let currB = Bundle.sh_bundleFor(aClass: aClass)
if let imgP = currB?.path(forResource: imgName, ofType: nil) {
return Self.init(contentsOfFile: imgP)
}
return nil
}
}
extension Bundle{
public static func sh_bundleFor(aClass:AnyClass) -> Self? {
let curBundle = Bundle.init(for: aClass)
if let curBundleName = curBundle.infoDictionary?["CFBundleName"] {
let curBundleDirectory = "\(curBundleName).bundle";
let bgdPath = curBundle .path(forResource: curBundleDirectory, ofType: nil)
let curr = Self.init(path: bgdPath!)
return curr
}
return nil
}
}