gpt4 book ai didi

iphone - 如何给MKMapView异步添加注解?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:48:21 25 4
gpt4 key购买 nike

我有很多注释要添加到 mkmapview 中。当我添加注释时,应用程序会短时间卡住。我知道主线程是唯一允许添加 UI 以查看的线程,如果这是真的,我如何让这个操作不卡住应用程序?

// in viewdidLoad
for (NSManagedObject *object in requestResults) {
CustomAnnotation *customAnnotation = [[CustomAnnotation alloc] init];
customAnnotation.title = object.title;
customAnnotation.coordinate = CLLocationCoordinate2DMake([object.latitude doubleValue], [object.longitude doubleValue]);
[_mapView addAnnotation:customAnnotation];
}
} // end viewdidload

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
// shows the annotation with a custom image
MKPinAnnotationView *customPinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"mapAnnotation"];
CustomAnnotation *customAnnotation = (id) annotation;
customPinView.image = [UIImage imageNamed:@"green"];
return customPinView;
}

最佳答案

您可以使用 Grand Central Dispatch - GCD为此。

尝试:

- (void)viewDidLoad
{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
for (NSManagedObject *object in requestResults)
{
CustomAnnotation *customAnnotation = [[CustomAnnotation alloc] init];
customAnnotation.title = object.title;
customAnnotation.coordinate = CLLocationCoordinate2DMake([object.latitude doubleValue], [object.longitude doubleValue]);
dispatch_async(dispatch_get_main_queue(), ^{
[_mapView addAnnotation:customAnnotation];
});
}
});
}

这是一个很好的教程:GCD and threading

关于iphone - 如何给MKMapView异步添加注解?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16537949/

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