gpt4 book ai didi

ios - MKOverlayView性能

转载 作者:行者123 更新时间:2023-12-01 16:58:37 24 4
gpt4 key购买 nike

我在 map 上添加了约3000 MKOverlays,并且可以想象,这需要一段时间,有时甚至需要八秒钟。我正在寻找一种使用线程来提高性能的方法,以便用户可以在添加叠加层时四处移动 map 。最好从 map 区域内的覆盖层开始依次添加覆盖层。我已经使用GCD尝试了以下方法:

- (MKOverlayView*)mapView:(MKMapView*)mapView viewForOverlay:(id)overlay {
__block MKPolylineView* polyLineView;

//do the heavy lifting (I presume this is the heavy lifting part, but
// because this code doesn't compile, I can't actually *test* it)
// on a background thread
dispatch_async(backgroundQueue, ^ {
polyLineView = [[[MKPolylineView alloc] initWithPolyline:overlay] autorelease];
[polyLineView setLineWidth:11.0];

//if the title is "1", I want a blue line, otherwise red
if([((LocationAnnotation*)overlay).title intValue]) {
[polyLineView setStrokeColor:[UIColor blueColor]];
} else {
[polyLineView setStrokeColor:[UIColor redColor]];
}

//return the overlay on the main thread
dispatch_async(dispatch_get_main_queue(), ^(MKOverlayView* polyLineView){
return polyLineView;
});
});
}

但是因为GCD块是使用 void参数和返回类型定义的,所以此代码不起作用-我在 return行上收到了不兼容的指针类型错误。我在这里缺少什么,还是另一种线程化方法?还是一种完全不同的方法来提高叠加添加过程的性能?我感谢所有帮助!

编辑:

我发现问题不在我实际在此处添加叠加层的地方:
for(int idx = 1; idx < sizeOverlayLat; idx++) {
CLLocationCoordinate2D coords[2];
coords[0].latitude = [[overlayLat objectAtIndex:(idx - 1)] doubleValue];
coords[0].longitude = [[overlayLong objectAtIndex:(idx - 1)] doubleValue];

coords[1].latitude = [[overlayLat objectAtIndex:idx] doubleValue];
coords[1].longitude = [[overlayLong objectAtIndex:idx] doubleValue];

MKPolyline* line = [MKPolyline polylineWithCoordinates:coords count:2];
[line setTitle:[overlayColors objectAtIndex:idx]];

[mapViewGlobal addOverlay:line];
}

在这里加所有3000可能需要100毫秒。在我展示的第一种方法中,需要很长时间(我认为)的部分是我实际创建叠加层的地方。

最佳答案

您想要的东西和编译器可以做什么之间有一点差距。当您调用dispatch_async时,实际上是在告诉CPU“在这里,拥有这段代码,并在需要时立即运行它,而不是现在,不阻塞我的用户界面线程”。但是,您的方法必须返回现在。您根本无法在后台线程中创建任何内容,因为mapView:viewForOverlay:返回之前,您将不得不等待它,因为它必须返回某些内容。

此方法不是使用GCD或任何背景代码的地方。如果您的问题是一次添加大量叠加层,那么我会将所有叠加层都分成100个大块,然后将它们添加到 map 中,每批之间的延迟为100毫秒。

关于ios - MKOverlayView性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9387951/

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