首页 > 代码库 > ios 加载.bundle文件里的图片
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文件里的图片