ios 加载.bundle文件里的图片
Posted 开花结果乐园
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios 加载.bundle文件里的图片相关的知识,希望对你有一定的参考价值。
+ (UIImage *)imageNamed:(NSString *)name ofBundle:(NSString *)bundleName {
static NSMutableDictionary *loadedObjectDict = nil;
if (!loadedObjectDict) {
loadedObjectDict = [[NSMutableDictionary alloc] init];
}
NSString *keyString = [NSString stringWithFormat:@"%@%@", bundleName, name];
RCDLiveWeakRef *ref = loadedObjectDict[keyString];
if (ref.weakRef) {
return ref.weakRef;
}
UIImage *image = nil;
NSString *image_name = [NSString stringWithFormat:@"%@.png", name];
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *bundlePath = [resourcePath stringByAppendingPathComponent:bundleName];
NSString *image_path = [bundlePath stringByAppendingPathComponent:image_name];
// NSString* path = [[[[NSBundle mainBundle] resourcePath]
// stringByAppendingPathComponent:bundleName]stringByAppendingPathComponent:[NSString
// stringWithFormat:@"%@.png",name]];
// image = [UIImage imageWithContentsOfFile:image_path];
image = [[UIImage alloc] initWithContentsOfFile:image_path];
[loadedObjectDict setObject:[RCDLiveWeakRef refWithObject:image] forKey:keyString];
return image;
}
以上是关于ios 加载.bundle文件里的图片的主要内容,如果未能解决你的问题,请参考以下文章
iOS-生成Bundle包-引入bundle-使用bundle