Swift NSBundle pathForResource()返回nil

编程入门 行业动态 更新时间:2024-10-07 10:24:36
本文介绍了Swift NSBundle pathForResource()返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的项目中有图像(仅在iPhone上支持,而在iPad上不支持).图像在一个组下,而不在images.xcassets下.

I have images in my project (which is only supported on iPhones, no iPads). The images are under a group and not under images.xcassets.

我想在所有iPhone上使用相同的图像,但6 Plus的@ 3x除外,所以我的结构如下:

I want to use the same image for all iPhones, except the @3x for the 6 Plus, so I have a structure as follows:

Project |_ Images_Group | |_ pic01@2x.png |_ pic01@3x.png

我正在使用以下代码在我的应用中使用它:

I am using this code to use it in my app:

if let path = NSBundle.mainBundle().pathForResource("pic01", ofType: "png") { image = UIImage(contentsOfFile: path) }

但是path总是返回nil.我需要pic01.png吗?

But path always returns nil. Do I need to have pic01.png?

该图像位于项目> 构建阶段> 复制捆绑资源下.我在做什么错了?

The image is present under Project > Build Phases > Copy Bundle Resources. What am I doing wrong?

推荐答案

此处要使用的工具是UIImage(named:),它将按照您的建议查找图像. pathForResource是通用函数,没有针对图像的特殊规则.它正在专门寻找pic01.png,它不存在.

The tool you want here is UIImage(named:), which will look up images the way you're suggesting. pathForResource is a generic function that doesn't have special rules for images. It's looking specifically for pic01.png, which does not exist.

更多推荐

Swift NSBundle pathForResource()返回nil

本文发布于:2023-11-26 23:32:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635660.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NSBundle   Swift   nil   pathForResource

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!