无法在 swift 2 中显示来自外部资产文件夹的 jpeg
Cannot display jpeg from outside asset folder in swift 2
这可能是我无知的问题,但我只是想按照下面的代码显示图像...。
我没有收到任何错误。该图像存在,但未出现在模拟器中。
如果我将路径字符串更改为不存在的图像,则会出现错误,因此我假设它正在正确找到图像。
我错过了什么?
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var hotspotImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let path = "/Users/Mel/Library/Developer/CoreSimulator/Devices/5760868E-BC01-4021-9500-1D4C09FE5106/data/Containers/Data/Application/2E99C508-30E5-4C83-BC72-CBF141A64769/Documents/tours/22/HI2.jpg"
let image = UIImage(contentsOfFile: path)
hotspotImage = UIImageView(image: image!)
}
}
而不是 hotspotImage = UIImageView(image: image!)
试试 hotspotImage.image = image!
.
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let getImagePath = paths.stringByAppendingString("filename")
testingImageView.image = UIImage(contentsOfFile: getImagePath)
您应该调用并提供文档目录的路径
let path = NSSearchPathForDirectoriesInDomains(
.DocumentDirectory, .UserDomainMask, true)[0] as NSString
let fileName = NSUserDefaults.standardUserDefaults()
.stringForKey("HI2.jpg") ?? defaultName
let imagePath = path.stringByAppendingPathComponent(fileName)!
let image = UIImage(contentsOfFile: imagePath )
// display the image
someImageView.image = image
这可能是我无知的问题,但我只是想按照下面的代码显示图像...。
我没有收到任何错误。该图像存在,但未出现在模拟器中。 如果我将路径字符串更改为不存在的图像,则会出现错误,因此我假设它正在正确找到图像。 我错过了什么?
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var hotspotImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let path = "/Users/Mel/Library/Developer/CoreSimulator/Devices/5760868E-BC01-4021-9500-1D4C09FE5106/data/Containers/Data/Application/2E99C508-30E5-4C83-BC72-CBF141A64769/Documents/tours/22/HI2.jpg"
let image = UIImage(contentsOfFile: path)
hotspotImage = UIImageView(image: image!)
}
}
而不是 hotspotImage = UIImageView(image: image!)
试试 hotspotImage.image = image!
.
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let getImagePath = paths.stringByAppendingString("filename")
testingImageView.image = UIImage(contentsOfFile: getImagePath)
您应该调用并提供文档目录的路径
let path = NSSearchPathForDirectoriesInDomains(
.DocumentDirectory, .UserDomainMask, true)[0] as NSString
let fileName = NSUserDefaults.standardUserDefaults()
.stringForKey("HI2.jpg") ?? defaultName
let imagePath = path.stringByAppendingPathComponent(fileName)!
let image = UIImage(contentsOfFile: imagePath )
// display the image
someImageView.image = image