NSPathStore2 的 UIImageView 实例泄露
UIImageView instances of NSPathStore2 leaked
我正在将 PNG 图像从 Document 目录加载到 UIImageView 中。使用 xcode 调试内存图 时会出现内存问题:
instances of NSPathStore2 leaked
听说仪器的泄密工具可以报假leaks。
内存好像上不去:
问题似乎出在 UIImage 中,参见 Instruments Leak 报告:
这是生成图像的代码:
try FileManager.default.copyfileToUserDocumentDirectory(forResource: "smiley", ofType: ".png")
var fullPathString = ""
if let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory,
.userDomainMask,
true).first {
let fileName = "smiley.png"
let fullDestPath = URL(fileURLWithPath: docDir).appendingPathComponent(fileName)
fullPathString = fullDestPath.path
}
mainStackView.addArrangedSubview(UIImageView(image: UIImage(contentsOfFile: fullPathString)))
mainStackView.addArrangedSubview(UIImageView(image: UIImage(contentsOfFile: fullPathString)))
可以在此处找到产生此问题的工作项目:UIImageViewNSPathStore2leaked
有没有办法更改代码以消除这些泄漏,或者这是那些虚假报告之一?
我也有这个问题。
重现
只需创建干净的新项目即可重现该问题。
- 拖image.png到项目,确保在Copy Bundle Resources.
- 将这两行写在viewDidLoad中。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let path = Bundle.main.path(forResource: "image", ofType: "png")!
let _ = UIImage(contentsOfFile: path)
}
}
- 构建并 运行 它。
- 当 viewController 出现时,单击 调试内存图 。
- 您可以看到泄漏问题。
不确定这是 init(contentsOfFile:)
的错误吗?
解决方案
我把API改为init(data:)
来解决问题init(contentsOfFile:)
let url = Bundle.main.url(forResource: "image", withExtension: "png")!
let imageData = try! Data(contentsOf: url)
let _ = UIImage(data: imageData)
使用 Xcode 11.5/11.3.1 和 Swift 5
似乎是 Instruments 中的错误,可能是误报。在 Xcode 12 beta 6 中,这不再显示为泄漏。
请注意,在 Xcode 11.6/11.7 中,“泄漏”也发生在 UIImage(named: )
方法中:
我正在将 PNG 图像从 Document 目录加载到 UIImageView 中。使用 xcode 调试内存图 时会出现内存问题:
instances of NSPathStore2 leaked
听说仪器的泄密工具可以报假leaks。
内存好像上不去:
问题似乎出在 UIImage 中,参见 Instruments Leak 报告:
这是生成图像的代码:
try FileManager.default.copyfileToUserDocumentDirectory(forResource: "smiley", ofType: ".png")
var fullPathString = ""
if let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory,
.userDomainMask,
true).first {
let fileName = "smiley.png"
let fullDestPath = URL(fileURLWithPath: docDir).appendingPathComponent(fileName)
fullPathString = fullDestPath.path
}
mainStackView.addArrangedSubview(UIImageView(image: UIImage(contentsOfFile: fullPathString)))
mainStackView.addArrangedSubview(UIImageView(image: UIImage(contentsOfFile: fullPathString)))
可以在此处找到产生此问题的工作项目:UIImageViewNSPathStore2leaked
有没有办法更改代码以消除这些泄漏,或者这是那些虚假报告之一?
我也有这个问题。
重现
只需创建干净的新项目即可重现该问题。
- 拖image.png到项目,确保在Copy Bundle Resources.
- 将这两行写在viewDidLoad中。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let path = Bundle.main.path(forResource: "image", ofType: "png")!
let _ = UIImage(contentsOfFile: path)
}
}
- 构建并 运行 它。
- 当 viewController 出现时,单击 调试内存图 。
- 您可以看到泄漏问题。
不确定这是 init(contentsOfFile:)
的错误吗?
解决方案
我把API改为init(data:)
来解决问题init(contentsOfFile:)
let url = Bundle.main.url(forResource: "image", withExtension: "png")!
let imageData = try! Data(contentsOf: url)
let _ = UIImage(data: imageData)
使用 Xcode 11.5/11.3.1 和 Swift 5
似乎是 Instruments 中的错误,可能是误报。在 Xcode 12 beta 6 中,这不再显示为泄漏。
请注意,在 Xcode 11.6/11.7 中,“泄漏”也发生在 UIImage(named: )
方法中: