美文网首页
转发: 从自定义bundle获取图片方法(imagedName

转发: 从自定义bundle获取图片方法(imagedName

作者: 金天动地 | 来源:发表于2019-07-13 17:58 被阅读0次

文件来源:https://www.jianshu.com/p/587c2b441b11
具体内容如下: 作者默认是png图片,其他类型图片注意要加后缀,而且亲测可用:

    // 方法1 直接通过imageName: 自定义bundel/文件名
    UIImage *image = [UIImage imageNamed:@"MyTest.bundle/Test"];
    
    // 方法2 通过 自定义bundle/文件名 获取path  最后调用图片路径方法imageWithContentsOfFile:
    NSString *file1 = [[NSBundle mainBundle] pathForResource:@"MyTest.bundle/Test" ofType:@"png"];
    UIImage *image1 = [UIImage imageWithContentsOfFile:file1];
    
    // 方法3  最麻烦: 自定义bundle路径 -> 获取自定义bundle -> 获取图片文件在自定义bundle路径,调用imageWithContentsOfFile:
    NSString *path = [[NSBundle mainBundle] pathForResource:@"MyTest" ofType:@"bundle"];
    NSBundle *bundle = [NSBundle bundleWithPath:path];
    NSString *file2 = [bundle pathForResource:@"Test" ofType:@"png"];
    UIImage *image2 = [UIImage imageWithContentsOfFile:file2];

1.先前以为图片只有在Assets文件下且是png格式 才能用imageName:方法,这次见识到,自定义bundle也可使用,强大!而且不仅针对png格式.
2.说明下bundle制作,直接创建文件,将要用资源拖入,然后改名字在器后面加上 ".bundle" (引号内为添加内容),还有通过Xcode创建Command+N

相关文章

网友评论

      本文标题:转发: 从自定义bundle获取图片方法(imagedName

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