美文网首页小问题bundle 文件搬砖
ios创建bundle的图片资源文件

ios创建bundle的图片资源文件

作者: 阿凡提说AI | 来源:发表于2016-11-07 11:09 被阅读2917次

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]

相关文章

网友评论

    本文标题:ios创建bundle的图片资源文件

    本文链接:https://www.haomeiwen.com/subject/iqfouttx.html