【问题标题】:Getting EXC_BAD_ACCESS when using UIImage(contentsOfFile: )使用 UIImage(contentsOfFile: ) 时获取 EXC_BAD_ACCESS
【发布时间】:2019-05-11 09:45:02
【问题描述】:

我目前正在使用UIImage(contentsOfFile: path),如果一个函数返回文件路径的图像,但它让我得到 EXC_BAD_ACCESS。 我进行了很多搜索,并在 Objective-C 中找到了一些关于自动发布和解决方案的内容,但我找不到 Swift 的任何内容。我应该在 Swift 中做什么?

【问题讨论】:

  • 我应该怎么做 – 显示代码。这个问题当然与内存管理无关。

标签: swift garbage-collection uiimage exc-bad-access autorelease


【解决方案1】:

检查零:

let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
if let dirPath = paths.first {
   let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent("Image2.png")
   let image    = UIImage(contentsOfFile: imageURL.path)
   // Do whatever you want with the image
}

【讨论】:

    猜你喜欢
    • 2019-05-26
    • 1970-01-01
    • 2016-08-05
    • 2023-03-12
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多