ios创建bundle的图片资源文件

1.首先,我们需要把用到的图片放到一个文件夹下面,然后更改这个文件夹的名字,在原来的文件名字后面添加.bundle,添加完毕之后,会出现一个弹出框,点击使用.bundel的按钮,


1840399-2afbb589fd73d6fb.png

点击之后,会生成一个带有.bundle的文件

1840399-de3e1760404c3168.png

或者

002glVnxty6GZINke4G8a&690.jpeg

2.图片获得bundle中的资源

UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
UIImage *image = [UIImage imageNamed:@"MyBundle.bundle/img_collect_success"];
[imgView setImage:image];

或者

UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
NSString *imgPath= [bundlePath stringByAppendingPathComponent :@"img_collect_success.png"];
UIImage *image_1=[UIImage imageWithContentsOfFile:imgPath];
[imgView setImage:image_1];

3.VC获得bundle中的资源

NSString * bundlePath = [[ NSBundle mainBundle] pathForResource: @ "MyBundle" ofType :@ "bundle"];
NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath];
UIViewController *vc = [[UIViewController alloc] initWithNibName:@"vc_name" bundle:resourceBundle];

当然,可以写成预编译语句:

#define MYBUNDLE_NAME @ "MyBundle.bundle"
#define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME]
#define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 如果你想将你开发的控件与别人分享,一种方法是直接提供源代码文件。然而,这种方法并不是很优雅。它会暴露所有的实现细节...
    小热狗阅读 8,833评论 3 9
  • (原文:How to Create a Framework for iOS作者:Sam Davies 译者:Mr_...
    Yaanco阅读 6,976评论 4 5
  • 原文链接在上一篇教程(中译版)中,你学到了怎么样创建一个可复用的圆形旋钮控件。然而你可能不清楚怎样让其他开发者更方...
    _冷忆阅读 7,118评论 9 5
  • 20161203
    宸丝喆时尚女装阅读 1,248评论 1 0
  • 这个名字取的还挺文艺!!!有点儿像人在囧途!今天终于体会到农民工返乡的感觉了! 今天赶了一天的车,从烟台坐...
    英伦小兔子阅读 1,495评论 1 1