user1058147
user1058147

Reputation: 11

MKAnnotationn leftcalloutaccessoryview is not getting refresh

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

Upvotes: 1

Views: 1553

Answers (1)

Guntis Treulands
Guntis Treulands

Reputation: 4762

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

Upvotes: 1

Related Questions