0

我在一个应用程序中,我得到Facebook的地方和MKMapView显示它们的使用方法如下注释工作。把Facebook的地方图像作为标注图像上的MKMapView

-(void) connectionDidFinishLoading: (NSURLConnection *) connection 
{ 
latitudeArray = [[NSMutableArray alloc]init]; 
longitudeArray = [[NSMutableArray alloc]init]; 
nameArray = [[NSMutableArray alloc]init]; 
placeImgArray = [[NSMutableArray alloc]init]; 

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 

NSDictionary *placesDict = [NSJSONSerialization JSONObjectWithData:empJsonData options:kNilOptions error:nil]; 
NSLog(@"COUNT = %i",placesDict.count); 
NSLog(@"Places Dictionary = %i",[[placesDict objectForKey:@"data"] count]); 

if ([[placesDict objectForKey:@"data"]count] >= 2) { 
    for (int i = 0; i<= [[placesDict objectForKey:@"data"] count] -1; i++) { 
     NSString *latitude = [[[[placesDict objectForKey:@"data"]objectAtIndex:i]objectForKey:@"location"]objectForKey:@"latitude"]; 

     NSString *longitude = [[[[placesDict objectForKey:@"data"]objectAtIndex:i]objectForKey:@"location"]objectForKey:@"longitude"]; 
     NSString *name = [[[placesDict objectForKey:@"data"]objectAtIndex:i]objectForKey:@"name"]; 
     imgURlStr = [[[placesDict objectForKey:@"data"]objectAtIndex:i]objectForKey:@"id"]; 

     [latitudeArray addObject:latitude]; 
     [longitudeArray addObject:longitude]; 
     [nameArray addObject:name]; 
     [placeImgArray addObject:imgURlStr]; 
     CLLocationCoordinate2D fbPlace; 
     fbPlace.latitude = [latitude doubleValue]; 
     fbPlace.longitude = [longitude doubleValue]; 
     Annotation *fbAnno = [[Annotation alloc]init]; 

     fbAnno.coordinate = fbPlace; 
     fbAnno.title = name; 
     [mapView addAnnotation:fbAnno]; 

    } 

} 

[mapView reloadInputViews]; 


} 

现在我需要与那些地方相关的图像,并把它们作为标注图像,而不是默认引脚。我怎样才能做到这一点 ??我尝试了下面的代码,但应用程序崩溃。

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
MKAnnotationView *pinView = nil; 
if(annotation != mapView.userLocation) 
{ 
    static NSString *defaultPinID = @"pin"; 
    pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
    if (pinView == nil) 
     pinView = [[MKAnnotationView alloc] 
        initWithAnnotation:annotation reuseIdentifier:defaultPinID]; 
    pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation 
               reuseIdentifier:defaultPinID] autorelease]; 
    UIButton *calloutButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    calloutButton.frame = CGRectMake(0, 0, 40, 20); 
    calloutButton.backgroundColor = [UIColor whiteColor]; 
    [calloutButton setTitle:@"Chatin" forState:UIControlStateNormal]; 
    calloutButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:12]; 

    calloutButton.titleLabel.textColor = [UIColor blackColor]; 

    pinView.rightCalloutAccessoryView = calloutButton; 

    //pinView.pinColor = MKPinAnnotationColorGreen; 
    pinView.canShowCallout = YES; 
    //pinView.animatesDrop = YES; 


    //////////////// Downloading Place Images from Facebook////////////////////// 



    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) { 


     NSString *placeImgURLStr = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?type=small",imgURlStr]; 

     // `imgURlStr` is `NSString` containing `id` of the Facebook place. 



     NSData *data0 = [NSData dataWithContentsOfURL:[NSURL URLWithString:placeImgURLStr]]; 
     UIImage *image = [UIImage imageWithData:data0]; 

     dispatch_sync(dispatch_get_main_queue(), ^(void) { 

      pinView.image = image; 
      pinView.hidden =NO; 

     }); 
    }); 

////////////////////////////////////////////////////////////////////////////////// 

} 
else { 
    [mapView.userLocation setTitle:@"I am here"]; 
} 

return pinView; 
} 

回答

0

您还没有共享你的崩溃的消息,所以我不得不猜测,这是关系到你使用在异步块全球imgURlStr当你真正想要使用的事实,与该注释相关联的imgURlStr。为此,您需要创建自己的注释子类并为其提供一个属性来存储imgURlStr值。然后,当viewForAnnotation叫你施放通用annotation回你的类(检查它是第一类,而不是用户位置),以及获取imgURlStr。

此外,行pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];撤销你在前面的行做了很好的工作。无论pinView是什么,它都会创建一个新的。您可以完全放弃该行。

+0

如果我使用'的NSString * placeImgURLStr = [NSString的stringWithFormat:@ “http://graph.facebook.com/%@/picture?type=small”,imgURlStr]; NSData * data0 = [NSData dataWithContentsOfURL:[NSURL URLWithString:placeImgURLStr]]; UIImage * image = [UIImage imageWithData:data0]; pinView.image = image; '没有使用GCD,它工作正常。但我遇到性能问题,因为所有下载都是在主线程上完成的。如果我使用GCD,它只会显示1个注释。 –