美文网首页
iOS swift pods 私有库 资源xib,png 加载

iOS swift pods 私有库 资源xib,png 加载

作者: 秋叶红90 | 来源:发表于2020-11-05 11:59 被阅读0次

    .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
        }
    }
    
    
    
    

    相关文章

      网友评论

          本文标题:iOS swift pods 私有库 资源xib,png 加载

          本文链接:https://www.haomeiwen.com/subject/vevovktx.html