I tried to load image from my app's sandbox path using
UIImage(contentsOfFile: path)
, but it causes memory leak issues by instances of NSPathStore2.
This is my testing example to reproduce it.
First, Create clean new project.
- Drag image.png to project, make sure it in Copy Bundle Resources.
- Write these two lines in viewDidLoad.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let path = Bundle.main.path(forResource: "image", ofType: "png")!
let _ = UIImage(contentsOfFile: path)
}
}
- Build and Run it.
- Click Debug Memory Graph when viewController appeared.
- You can see leak issues by NSPathStore2.
Not sure is it a bug of
init(contentsOfFile:)
?
Now, I tried to load image by UIImage(data: imageData) likes below to avoid leaked issues
let url = Bundle.main.url(forResource: "image", withExtension: "png")!
let imageData = try! Data(contentsOf: url)
let _ = UIImage(data: imageData)
Using Xcode 11.5 / 11.3.1 and Swift 5
Does anyone know it is normal or bug issues?
This is a post link from stackoverflow.