iOS 组件化加载 图片、gif、xib等文件(OC/Swift)

前言

xcode 15.3
ios 10.0

起因,因为公司要使用保利威视的视频使用,我拿来他们的Module来封装做成组件来用,但是他们的xib文件一直是nil,为此困扰了我好久

说明

在组件化中,你的.assets中的图片,文件夹中的图片、gif图片,xib文件,json文件等都需要进行处理,否则找不到文件,加载不了,都会为nil,

1、在podspec设置生成bundle文件

在你的组件化文件pod -> .podspec文件中设置
PolyvOpenSourceModule/Assets.xcassets: 设置加载Assets.xcassets中的图片
PolyvOpenSourceModule//.xib:设置加载xib文件
PolyvOpenSourceModule/Images/
: 设置加载Module -> Images文件夹中的图片、gif图片、json文件等

  s.resource_bundles = {
    'Polyv_bundles' => ['PolyvOpenSourceModule/Assets.xcassets', 'PolyvOpenSourceModule/**/*.xib', 'PolyvOpenSourceModule/Images/*']
  }

2、在组件中加载文件

2.1、Objective-C

2.1.1 首先生成 NSBundle文件

FADeviceAssets.h 封装

+ (NSBundle *)hj_ResourceBundle;

FADeviceAssets.m 注意,此处的bundle文件名要和 .podspec 文件中设置的一致

static NSBundle *resourceBundle = nil;
static dispatch_once_t onceToken;

+ (NSBundle *)hj_ResourceBundle {
   dispatch_once(&onceToken, ^{
       NSString *resourceBundlePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"Polyv_bundles" ofType:@"bundle"];
       resourceBundle = [NSBundle bundleWithPath:resourceBundlePath];
   });
   return resourceBundle;
}

2.1.2 加载图片

封装加载方法

@interface UIImage (Category)
+ (instancetype)hj_imageDevice:(NSString *)name;
@end


@implementation UIImage (Category) 
+ (instancetype)hj_imageDevice:(NSString *)name {
   return [UIImage imageNamed:name inBundle:[FADeviceAssets hj_ResourceBundle] compatibleWithTraitCollection:nil];
} 
@end

图片加载

UIImageView * imageView = [[UIImageView alloc] init];
imageView.image = [UIImage hj_imageDevice:@"FADevice_second_bg"]; 

2.1.3 gif 图片加载

此处我使用的是 FLAnimatedImage 加载本地gif

@interface FLAnimatedImage (Category)
+ (instancetype)hj_gif:(NSString *)name;
@end

@implementation FLAnimatedImage (Category) 
+ (instancetype)hj_gif:(NSString *)name {
    NSBundle * subBundle = [FADeviceAssets hj_ResourceBundle];
     
    NSURL * url = [subBundle URLForResource:name withExtension:@"gif"];
    NSData * imageData = [NSData dataWithContentsOfURL:url];
    return [FLAnimatedImage animatedImageWithGIFData:imageData];
} 
@end

加载方法

 FLAnimatedImageView * imageView = [[FLAnimatedImageView alloc] init];
 imageView.image = [UIImage hj_imageDevice:@"FADevice_second_auto"];

2.1.4 解析json

NSURL * url = [[FADeviceAssets hj_ResourceBundle] URLForResource:name withExtension:@"json"];
NSData * imageData = [NSData dataWithContentsOfURL:url];
NSDictionary * json = [NSJSONSerialization JSONObjectWithData:imageData options:NSJSONReadingMutableContainers error:nil];

2.1.5 加载 xib

此处也是我遇到的问题所在,头都给快给我整秃了

此处是原来加载的xib代码,无法加载

PLVVodPlayerSkin *skin = [[PLVVodPlayerSkin alloc] initWithNibName:nil bundle:nil];

更改后的

PLVVodPlayerSkin *skin = [[PLVVodPlayerSkin alloc] initWithNibName:@"PLVVodPlayerSkin" bundle:[FADeviceAssets hj_ResourceBundle]];

2.2 Swift

2.2.1 swift 加载图片

注意,此处的bundle文件名要和 .podspec 文件中设置的一致, 上面有说明,此处就不再次说明

extension UIImage {
    class func yl_image(named name: String) -> UIImage{
        return YLStudyAssets.bundledImage(named: name)
    }
}

open class YLStudyAssets: NSObject {
    internal class func bundledImage(named name: String) -> UIImage {
        let primaryBundle = Bundle(for: YLStudyAssets.self)
        if let image = UIImage(named: name, in: primaryBundle, compatibleWith: nil) {
            return image
        } else if
            let subBundleUrl = primaryBundle.url(forResource: "Polyv_bundles", withExtension: "bundle"),
            let subBundle = Bundle(url: subBundleUrl),
            let image = UIImage(named: name, in: subBundle, compatibleWith: nil){
            return image
        }
        return UIImage()
    }

}

图片加载

let imageView = UIImageView()
imageView.image = UIImage.yl_image(named: "")

2.2.2 加载gif图片

此处使用FLAnimatedImage加载,注意,此处的bundle文件名要和 .podspec 文件中设置的一致

import FLAnimatedImage
extension FLAnimatedImage {
    class func yl_gif(named name: String) -> FLAnimatedImage {
        return YLStudyAssets.bundleGif(named: name)
    }
}

open class YLStudyAssets: NSObject {
    internal class func bundleGif(named name: String) -> FLAnimatedImage {
        let primaryBundle = Bundle(for: YLStudyAssets.self)
        
        if let subBundleUrl = primaryBundle.url(forResource: "YLStudy_bundles", withExtension: "bundle"),
           let subBundle = Bundle(url: subBundleUrl),
           let url = subBundle.url(forResource: name, withExtension: "gif"),
           let imageData = try? Data(contentsOf: url) { 
                return FLAnimatedImage(animatedGIFData: imageData)
        }
        return FLAnimatedImage()
    }
}

gif图片加载

    private lazy var imageIcon: FLAnimatedImageView = {
        let imageView = FLAnimatedImageView() 
        imageView.animatedImage = FLAnimatedImage.yl_gif(named: "voiceLeft")
        
        return imageView
    }()

2.2.3 加载json

我此处是使用lottie-ios 加载动图,如果你要加载json文件,自己拆开做吧,按照oc处理,应该不难的

let primaryBundle = Bundle(for: YLHomeAssets.self)
let subBundleUrl = primaryBundle.url(forResource: "YLHome_bundles", withExtension: "bundle")
let subBundle = Bundle(url: subBundleUrl!) ?? Bundle.main 
let animation = Animation.named(fileName, bundle: subBundle)

其余有空补充

你可能感兴趣的:(iOS 组件化加载 图片、gif、xib等文件(OC/Swift))