码迷,mamicode.com
首页 > 移动开发 > 详细

ios 加载.bundle文件里的图片

时间:2017-03-11 11:44:43      阅读:1135      评论:0      收藏:0      [点我收藏+]

标签:load   init   off   append   nil   undle   ret   loaded   mat   

+ (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文件里的图片

标签:load   init   off   append   nil   undle   ret   loaded   mat   

原文地址:http://www.cnblogs.com/lsh1234/p/6534419.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!