我试图添加带有自定义图像的导航栏按钮。但是,无论我使用哪种方法,图像总是看起来被拉伸。
方法1:
let barbuttonitem = UIBarButtonItem(image: UIImage(named: "apps_small"), style: .plain, target: self, action: nil)
navigationItem.leftBarButtonItem = barbuttonitem
Run Code Online (Sandbox Code Playgroud)
看起来像这样:
方法2:
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "apps_small"), for: .normal)
button.addTarget(self, action: #selector(TeachingRootViewController.appsTouched), for: .touchUpInside)
button.frame = CGRect(x: 0, y: 0, width: 10, height: 10)
button.bounds = CGRect(x: 0, y: 0, width: 10, height: 10)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)
Run Code Online (Sandbox Code Playgroud)
看起来像这样:
方法3:
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "apps_small"), for: .normal)
button.setTitle("Button", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 10, height: …Run Code Online (Sandbox Code Playgroud) 如果您创建以下视图的列表作为行:
struct ImageCellView: View {
let image: UIImage
init(image: UIImage) {
self.image = image
}
var body: some View {
return Image(uiImage: image).frame(height: 128).clipped()
}
}
Run Code Online (Sandbox Code Playgroud)
删除单元格后,UIImage 的内存不会释放,直到添加新单元格。查看内存图,我们可以发现 AGGraphStorage 持有对此的引用:
即使添加新单元格可以以某种方式释放内存,UIImage 偶尔也会泄漏:
这是 SwiftUI 中的错误吗?有什么办法可以摆脱这种行为吗?