我正在尝试从网址添加图片。我正在使用Thread来加载图像。我将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];
}
答案 0 :(得分:1)
好的,我想我终于为你找到了它:)
我做的是 - 我创建了一个按钮,将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];
}