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 値を格納するプロパティを与える必要があります。次に、viewForAnnotationが呼び出されたときに、ジェネリックannotationをクラスにキャストして戻し (ユーザーの場所ではなく、最初にそのクラスであることを確認してください)、imgURlStr を取得します。

また、この行pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];は、前の行で行った優れた作業を元に戻します。pinView が何であれ、新しいものを作成します。その行を完全に削除できます。

于 2012-12-29T08:21:54.160 に答える