0

我正在开发一个应用程序,我在其中获取 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;
}
4

1 回答 1

0

您还没有分享您的崩溃消息,所以我不得不猜测它与您在该异步块中使用全局 imgURlStr 的事实有关,而实际上您想使用连接到那个的 imgURlStr注解。为此,您需要创建自己的注释子类并为其提供一个属性来存储 imgURlStr 值。然后,当viewForAnnotation被调用时,您将泛型annotation转换回您的类(首先检查它是该类而不是用户位置)并检索 imgURlStr。

该行还pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];取消了您在前几行中所做的出色工作。不管 pinView 是什么,它都会创建一个新的。你可以完全放弃这条线。

于 2012-12-29T08:21:54.160 回答