2

何が間違っているのかわかりませんが、うまくいきません。

mapView: viewForAnnotation:はこのように見えます:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKAnnotationView *aView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"ImageMapView"];
    if (!aView)
    {
        aView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ImageMapView"];
        aView.canShowCallout = YES;
        aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        aView.leftCalloutAccessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    }
    [(UIImageView *)aView.leftCalloutAccessoryView setImage:nil];
    aView.annotation = annotation;
    return aView;
}

したがって、すべての新しい注釈は、左側のコールアウトの空白の画像ビューで始まります。

コールアウトに触れると、次のコードが実行されます。

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
    //loading an image
    [(UIImageView *)view.leftCalloutAccessoryView setImage:image];
}

そして、ロギングの助けを借りて、画像のURLとデータは問題なく、画像自体はnilではないことがわかりましたが、左側のコールアウトには何も表示されません。

誰かが私にこの問題の原因を突き止める方法を提案できますか?

編集

いくつかのログを追加したので、何が起こっているのかを確認できます。

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
    UIImage *image = [self.delegate imagesMapViewController:self thumbnailForAnnotation:view.annotation];
    NSLog(@"Image was: %@", [(UIImageView *)view.leftCalloutAccessoryView image]);
    NSLog(@"Image received: %@", image);
    [(UIImageView *)view.leftCalloutAccessoryView setImage:image];
    NSLog(@"Image became: %@", [(UIImageView *)view.leftCalloutAccessoryView image]);
}

そして私は次の出力で終わります:

Image was: (null)
Image received: <UIImage: 0x7933470>
Image became: <UIImage: 0x7933470>

そのため、画像は設定されていますが、表示されていません。理由はわかりません。また、最初のログ情報は2番目の注釈ビューがテープに記録された後にのみ表示されるため、最初の注釈ビューが呼び出していないことは非常にまれです。mapView: didDeselectAnnotationView:

入れても

[(UIImageView *)aView.leftCalloutAccessoryView setImage:[self.delegate imagesMapViewController:self thumbnailForAnnotation:aView.annotation]];

それ以外の

[(UIImageView *)aView.leftCalloutAccessoryView setImage:nil];

左のmapView: viewForAnnotation:すべてのコールアウトアクセサリには、必要な画像がありますが、オフソースで画像をオンデマンドでダウンロードしたい

4

4 に答える 4

5

問題は、あなたが使用することです

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view

いつ使うべきか

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view

乾杯 )

于 2012-05-05T09:08:13.603 に答える
2

I don't think your didSelectAnnotationView method will work if you're loading the image from a variable. If so, you'll need to refer to the original annotation:

-(void) mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    MyAnnotation *pin = (MyAnnotation *) [view annotation];
    view.leftCalloutAccessoryView =  [[UIImageView alloc] initWithImage:[UIImage imageNamed:pin.flag]];

}

Hope this enhances the original answer... :)

于 2013-01-12T06:27:46.887 に答える
1

In mine, I have a push segue from the MapViewController to the UIImageView with Identifier set for the Push segue.

I have this protocol in MapViewController in .h:

    @protocol MapViewControllerDelegate <NSObject>
    -(void)mapViewController:(MapViewController *)sender imageForAnnotation:(MKAnnotationView *)annotation;
    -(void)mapViewController:(MapViewController *)sender annotationSegue:(id <MKAnnotation>)annotation;
    @end

Then I define the delegate in the MapViewController.h @interface:

    @property (nonatomic, weak) id <MapViewControllerDelegate> delegate;

You may not need the required protocol methods above. I'm not sure.

I'm seeing that the difference between your viewforAnnotation and mine is I have two lines flipped:

    aView.annotation = annotation;
    [(UIImageView *)aView.leftCalloutAccessoryView setImage:nil];

After this method, you see the map and select the pin. Then it goes to didSelectAnnotioatnView as I show above with the delegate.

I also have a PhotoListTableViewController that has this method:

    -(void)mapViewController:(MapViewController *)sender imageForAnnotation:(MKAnnoatioatnView *)annotation
    {
        dispatch_queue_t downloadQueue = dispatch_queue_create ("flickrUrls", NULL);
        dispatch_asynch(downloadQueue, ^{
            FlickrPhotoAnnotation *fpa = (FlickrPhotoAnnotation *) annotation.annotation;
            NSURL *url = [FlickrFetcher urlForPhoto:fpa.phot format:FlickrPhotoFormatSquare];
            NSData *data = [NSData dataWithContentsOfURL:url];
            dispatch_async(dispatch_get_main_queue(), ^{
                UIImage *image = [UIImage imageWithData:data];
                [(UIImageView *)annotation.leftCalloutAccessoryView setImage:image];
            });
        });
        dispatch_release(downloadQueue);
    }

So I believe this shows the root cause (above). So you can see, the above sets the image. The method you had initializes it to nil to start out. Also, my PhotoListTableViewController that implements the above method in the .m part of the @interface you see

    @interface PhotoListTableViewController()<MapViewControllerDelegate>
于 2012-05-04T23:42:51.947 に答える
0

I just did this:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {

    if([view.annotation isKindOfClass:[MyLocation class]]) {

        __weak MKAnnotationView *weakView = view;

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            NSData *data = [NSData dataWithContentsOfURL:((MyLocation*)weakView.annotation).thumbnailUrl];
            UIImage *image = [UIImage imageWithData:data];

            dispatch_async(dispatch_get_main_queue(), ^ {

                UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
                weakView.leftCalloutAccessoryView = imgView;
            });
        });
    }
}

This means that it just starts to download it when you what it, and when its done, it animates the view to make room for the image.

于 2015-02-19T03:50:16.300 に答える