无法在 Playground Xcode 7.2.1 中访问图像资源

Image resource not accessible in Playground Xcode 7.2.1

我在 swift 游乐场 Xcode 7.2.1

中加载图像时遇到问题

同上,此处重复

import UIKit

let image = UIImage(named: "sample")
let path = NSBundle.mainBundle().pathForResource("sample", ofType: "png")

文件 sample.png 位于 playground 的 Resources 文件夹中。

我错过了什么吗? 有什么改变吗? 这是 Xcode 7.2.1 中的错误吗?

您的图像不在资产目录(或 PNG)中,您需要将完整的文件名传递给 UIImage named 构造函数:

let image = UIImage(named: "sample.jpg")

pathForResource中,您的文件类型有误。您的图片的扩展名是 jpg,而不是 png:

let path = NSBundle.mainBundle().pathForResource("sample", ofType: "jpg")