你好,游客 登录 注册 搜索
背景:
阅读新闻

iOS的Bundle资源束制作

[日期:2013-03-05] 来源:Linux社区  作者:nono_love_lilith [字体:大 中 小]

4.读取和使用文件。

    UILabel *la = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 50, 50)];
    la.text = @"陈凯";
    NSString *image_url = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"nono.bundle/images/Android.png"];
    la.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:image_url]];

此时,因为以前我们获取UIImage对象大部分都是直接使用的api为:

[UIImageimageNamed:@"android.png"]

方法其实也是通过相对路径给我们找到了图片资源。

但是在我们自己制作的bundle文件夹的图片资源则不能使用该方法来找到了。

我们每次指定图片需要传入一个很长的地址,觉得比较麻烦。但是我们会发现,图片都是在xxx.bundle/images下面。

那么我们也可以使用宏或是类别来定义一个方法来简单到和[UIImageimageNamed:@"android.png"]一样。只需一个文件名就行了。

宏的表现可以参考three20下的

TTIMAGE(@"bundle://Three20.bundle/images/backIcon.png")

返回的就是一个image对象。

three20框架中其实是使用了scheme的思想,bundle://和document;//来加载不同文件夹下的资源。

加载自定义bundle文件夹下的图片最后就是根据

先获取应用的绝对路径

mainbundle_path = [NSBundlemainBundle] resourcePath] ;

然后截取了bundle://后面的path 和mainbundle_path组装成最后的地址。

对于我们,简单的实现可以直接使用类别

-(UIImage*) imagesNamedFromCustomBundle:(NSString *)name
{
    NSString *main_images_dir_path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"nono.bundle/images"];
//    NSAssert(main_images_dir_path, @"main_images_dir_path is null");
    NSString *image_path = [main_images_dir_path stringByAppendingPathComponent:name];
    return [UIImage imageWithContentsOfFile:image_path];
}

创建一个UIImage的类别,添加这个扩展的方法。

----end---

linux
相关资讯       iOS Bundle 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款