gpt4 book ai didi

ios - MKMapSnapshotter 内存崩溃 - UITableView 中的多次调用

转载 作者:行者123 更新时间:2023-11-29 00:50:00 25 4
gpt4 key购买 nike

我们有一个 iOS 应用程序,它有一个 UITableView,其中包含在单元格出现时生成的 map 快照。我们使用的示例列表只是显示基于模型类中提供的纬度/经度的 map 快照。我开始注意到内存崩溃,所以我将代码减少到最低限度。当我们只做快照而不对结果做任何事情时,崩溃仍然会发生。请参阅下面的代码,它包含在我们的自定义单元格中并通过 cellForItemAtIndexPath 方法调用:

private func testMapSnapshot(viewModel: StreamViewModel)
{
let latDelta:CLLocationDegrees = 0.005
let lonDelta:CLLocationDegrees = 0.005

let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(viewModel.coordinate.latitude, viewModel.coordinate.longitude)
let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)

let options = MKMapSnapshotOptions()
options.region = region
options.size = mapImageView.frame.size
options.scale = UIScreen.mainScreen().scale

viewModel.mapSnapshotter = MKMapSnapshotter(options: options)
viewModel.mapSnapshotter!.startWithCompletionHandler() { snapshot, error in
// do nothing
}
}

didEndDisplayingCell 中,我确保取消 mapSnapshotter。请参阅引用资料(我们将模型列表保留在包含 tableview 的主 VC 类中):

func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) 
let model = viewModel?[indexPath.item] {
model.mapSnapshotter?.cancel()
model.mapSnapshotter = nil
}
}

请注意,在执行最后一步之前,它很早就崩溃了。但是现在,如果您开始快速向下滚动列表,它就会开始卡顿并且不会停止卡顿。如果您在大约 150 行的列表中上下移动,我们将在不到 30 秒的时间内开始看到内存警告然后崩溃。

我通过 Instruments 运行了它,但它不是很有帮助。看起来 Heap 和 Anonymous VM 分配正在逐渐增加,可能导致崩溃。请参阅引用:

Instruments Screenshot

我看到了这个帖子: MKMapSnapshotter uses incredible amounts of CPU & RAM但它没有答案,也没有真正解决为什么内存不会被释放。

关于在何处进行此操作的任何想法?在此先感谢您,如果我可以提供更多信息,请告诉我。

最佳答案

虽然我无法找到解决这个特定问题的方法,但我能够通过仅在 ScrollView 停止时调用 map 快照程序来解决它 - 然后它会抓取所有可见的单元格并仅加载这些单元格。这样,它可以最大限度地减少对此 API 的调用次数并防止内存问题,而不是在您通过 cellForRow 方法向下滚动列表时不断调用它。

关于ios - MKMapSnapshotter 内存崩溃 - UITableView 中的多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38250875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com