MKAnnotationn leftcalloutaccessoryview 未刷新

发布于 2024-12-17 12:24:35 字数 1290 浏览 0 评论 0原文

我正在尝试从 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];
}

I am trying to add an image from a URL to. I am using a Thread to load image. i am passing MKAnnotationView object to another method and loading image in that method. When i click on the annotation first time image is loading but not showing on the annotation but when i click again on second time it is showing. I am unable to find what is the error.

-(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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一袭水袖舞倾城 2024-12-24 12:24:35

好吧,我想我终于为你弄清楚了:)

我所做的是 - 我创建了一个按钮,将 ActivityIndi​​cator 添加到其中作为子视图,当下载图像时,我将图像设置为同一个按钮。并删除活动指示器。在我这边工作。这是我最终得到的代码:

-(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];
}

Ok, I think I finally figured it out for You :)

What I did was - I created a button, add to it activityIndicator as a subview, and when there is image downloaded, I set up the image to the same button. And remove activity indicator. Works on my side. Here is code I ended up with:

-(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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文