ios - 如何获取xcode项目中文件夹的目录? ios swift

标签 ios iphone xcode swift

我在名为“Tiles”的文件夹中有一堆瓷砖图像

我需要一次从该文件夹中提取一张图像,并将其设置为 UIView 对象。

我对如何从该文件夹中获取图像感到困惑。

谁能用 Swift 帮忙做这件事?

enter image description here

最佳答案

如果你有图片名称,你可以像这样访问这个文件夹中的图片

let image = UIImage(named: "image.png")

或者这个

let imageURL = NSBundle.mainBundle().URLForResource("image", withExtension: "png")
let image = UIImage(contentsOfFile: imageURL!.path!)

如果你想获取所有图片,你在项目中创建一个真正的文件夹

 let imageArray = NSBundle.mainBundle().URLsForResourcesWithExtension("png", subdirectory: "Titles") as! [NSURL]

这将在 Titles 文件夹中返回 png 的 urlArray。您可以使用 contentsOfFile 如前所示访问图像

关于ios - 如何获取xcode项目中文件夹的目录? ios swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30595282/

相关文章:

iphone - viewWillAppear 困境

iphone - 我应该如何在从 UISlider 读取的核心数据中存储百分比值?

iphone - 在 UIPopoverController 的页脚中使用 UISegmentedControl

ios - 当 UITableView 和 UIImageView 出现在同一 View Controller 上时如何正确使用约束

ios - ld : framework not found FileProvider for architecture x86_64 in Xcode 8. 3.2

ios - UITextField 子类,下方有栏 : bar not going all the way

ios - 如何在不安装整个 pod 的情况下安装 pod 的子规范?

ios - Swift 编译器错误 : Cannot find 'ClassName.h' in scope - Xcode 13

xcode - xcode快捷方式转到下一个编译器错误(忽略警告!)

ios - 如何在 UINavigationBar 上设置图像