2011-11-21 17 views
2

我想從一個URL添加圖像到。我正在使用線程來加載圖像。我將MKAnnotationView對象傳遞給另一個方法並在該方法中加載圖像。當我點擊註釋第一次圖像加載,但沒有顯示註釋,但當我再次點擊第二次顯示。我無法找到錯誤。MKAnnotationn leftcalloutaccessoryview沒有得到刷新

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

回答

2

好吧,我想我終於想出了你:)

我所做的就是 - 我創建了一個按鈕,添加到它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]; 
}