我正在尝试从URL添加图像。我正在使用线程加载图像。我将MKAnnotationView对象传递给另一个方法并在该方法中加载图像。当我第一次单击注释时,图像正在加载但未显示在注释中,但是当我再次单击时,图像正在显示。我找不到错误所在。
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
NSLog(@"Selected View Tag = %d", view.tag);
UIImageView *imaged = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,31,31)];
act = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0,0,31,31)];
[act startAnimating];
[imaged addSubview:act];
view.leftCalloutAccessoryView = imaged;
[NSThread detachNewThreadSelector:@selector(threadaimage:) toTarget:self withObject:view];
}
-(void)threadaimage:(MKAnnotationView*)imageview1
{
NSLog(@"Selected Tag in Thread Method = %d",imageview1.tag);
UIButton *imag = [[UIButton alloc]init];
imag.frame = CGRectMake(0, 0, 31, 31);
NSData *imageData = [NSData dataWithContentsOfURL:
[NSURL URLWithString:[image_arr objectAtIndex:imageview1.tag]]];
NSLog(@"%@",[image_arr objectAtIndex:imageview1.tag]);
[imag setImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal];
[self.view addSubview:imag];
imageview1.leftCalloutAccessoryView = imag;
[act stopAnimating];
}
最佳答案
好的,我想我终于为您解决了这个问题:)
我所做的是-创建了一个按钮,并向其中添加了activityIndicator作为子视图,下载图像后,将图像设置为相同的按钮。并删除活动指示器。在我这边工作。这是我最终得到的代码:
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
UIButton *customAccessoryButton = [UIButton buttonWithType:UIButtonTypeCustom];
customAccessoryButton.frame = CGRectMake(0, 0, 31, 31);
act = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0,0,31,31)];
[act startAnimating];
[customAccessoryButton addSubview:act];
view.leftCalloutAccessoryView = customAccessoryButton;
[NSThread detachNewThreadSelector:@selector(threadaimage:) toTarget:self withObject:view];
}
- (void)threadaimage:(MKAnnotationView*)imageview1
{
UIButton *customAccessoryButton = (UIButton*)imageview1.leftCalloutAccessoryView;
NSData *imageData = [NSData dataWithContentsOfURL:
[NSURL URLWithString:@"http://www.gemini.edu/images/stories/press_release/pr2008-6/fig1.jpg"]];
[customAccessoryButton setImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal];
[act removeFromSuperview];
[act stopAnimating];
}
关于iphone - MKAnnotationn leftcalloutaccessoryview没有刷新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8214860/