将图像加载到 TableView 单元格时,我遇到了严重的内存问题。使用 autoreleasepool 纠正问题的正确方法是什么?我尝试过:
for i in 0 ..< 5 {
autoreleasepool {
for j in 0 ..< 1000 {
image = UIImage(data: data)
dispatch_async(dispatch_get_main_queue(), {
if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
// cellToUpdate.imageView?.image = image
cell.imageView!.image = image
}
})
}
}
}
最佳答案
我在代码中使用了这种结构,它将帮助您解决问题:
func tooManyPictures() {
let file = pathForResourceInBundle
for _ in 0 ..< 5 {
autoreleasepool {
for _ in 0 ..< 1000 {
let image = NSImage(contentsOfFile: file)
}
}
}
}
关于xcode - Swift 正确的自动释放池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29313387/