使用bundle打包圖片資源和xib

1、如何創(chuàng)建一個(gè)bundle:

iOS 如何把圖片資源打包成bundle文件及遇到的坑

2、把圖片資源,xib打包進(jìn)bundle以后,如何使用?

2.1、加載本地的bundle

+ (NSBundle *)bundle {
    static NSBundle *bundle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        bundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"ZLYKit" ofType:@"bundle"]];
    });
    return bundle;
}

2.2、獲取bundle里面的圖片資源

+ (UIImage *)imageNamed:(NSString *)name {
    UIImage *image = [UIImage imageNamed:name inBundle:self.bundle compatibleWithTraitCollection:nil];
    return image;
}

+ (UIImage *)imageNamed:(NSString *)name type:(NSString *)type {
    if (name.length == 0) return nil;
    int scale = (int)UIScreen.mainScreen.scale;
    if (scale < 2) scale = 2;
    else if (scale > 3) scale = 3;
    NSString *n = [NSString stringWithFormat:@"%@@%dx", name, scale];
    UIImage *image = [UIImage imageWithContentsOfFile:[self.bundle pathForResource:n ofType:type]];
    if (!image) image = [UIImage imageWithContentsOfFile:[self.bundle pathForResource:name ofType:type]];
    return image;
}

[self.leftImageView sd_setImageWithURL:[NSURL URLWithString:fileModel.courseImageUrl] placeholderImage:[ZLYUtilities imageNamed:@"place_holder_home_lesson"]];

2.3、獲取bundle里面的xib資源

[_tableView registerNib:[UINib nibWithNibName:@"LYChatRoomImageTableViewCell" bundle:[ZLYUtilities bundle]] forCellReuseIdentifier:imageID];
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容