1

我正在关注关于 iOS 5.0 开发的优秀 CS193P 讲座:http:
//www.stanford.edu/class/cs193p/cgi-bin/drupal/

我现在在分配#5,要求的任务#5b。当我单击地图上的注释时,我需要在其中显示带有缩略图的标注。

我设法用以下代码(使用 Flickr)毫无问题地做到了这一点。

MapViewController.h:

@class MapViewController;

@protocol MapViewControllerDelegate <NSObject>

- (UIImage *)mapViewController:(MapViewController *)sender imageForAnnotation:(id <MKAnnotation>)annotation;

@end

@interface MapViewController : UIViewController

@property (nonatomic, strong) NSArray *annotations; // of id <MKAnnotation>
@property (nonatomic, weak) id <MapViewControllerDelegate> delegate;

@end

MapViewController.m:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)annotationView
{
    if ([annotationView.leftCalloutAccessoryView isKindOfClass:[UIImageView class]]) {
        UIImage *image = [self.delegate mapViewController:self imageForAnnotation:annotationView.annotation];
        [(UIImageView *)annotationView.leftCalloutAccessoryView setImage:image];
    }
}

PhotoListTableViewController.m:

@interface PhotoListTableViewController() <MapViewControllerDelegate>

- (UIImage *)mapViewController:(MapViewController *)sender imageForAnnotation:(id <MKAnnotation>)annotation
{
    UIImage *image = nil;
    if ([annotation isKindOfClass:[FlickrAnnotation class]]) { // make sure the annotation is a FlickrAnnotation
        FlickrAnnotation *flickrAnnotation = (FlickrAnnotation *)annotation;
        NSURL *photoUrl = [FlickrFetcher urlForPhoto:flickrAnnotation.photo format:FlickrPhotoFormatSquare];
        image = [UIImage imageWithData:[NSData dataWithContentsOfURL:photoUrl]];
    }
    return image;
}

@end

我在 MapViewController 的 mapView:didSelectAnnotationView: 方法中使用一个委托来从另一个控制器 PhotoListTableViewController 检索图像(以确保我有一个通用的 MapViewController)。

它工作正常......但我需要修改此代码以在另一个线程中调用 Flickr。所以我为此修改了 mapViewController:imageForAnnotation: 方法:

- (UIImage *)mapViewController:(MapViewController *)sender imageForAnnotation:(id <MKAnnotation>)annotation
{
    __block UIImage *image = nil;
    if ([annotation isKindOfClass:[FlickrAnnotation class]]) { // make sure the annotation is a FlickrAnnotation
        FlickrAnnotation *flickrAnnotation = (FlickrAnnotation *)annotation;
        dispatch_queue_t downloadQueue = dispatch_queue_create("ThumbnailDownloader", NULL);
        dispatch_async(downloadQueue, ^{
            NSURL *photoUrl = [FlickrFetcher urlForPhoto:flickrAnnotation.photo format:FlickrPhotoFormatSquare];
            dispatch_async(dispatch_get_main_queue(), ^{ // execute on the main thread
                image = [UIImage imageWithData:[NSData dataWithContentsOfURL:photoUrl]];
            });
        });
        dispatch_release(downloadQueue);
    }
    return image;
}

但是,使用此代码时,不会显示缩略图。我知道这是因为当方法“返回图像”时,图像仍然为零导致 downloadQueue 线程尚未完成。我试图像这样在块内“返回图像”:

return [UIImage imageWithData:[NSData dataWithContentsOfURL:photoUrl]];

但编译器不喜欢这样:

将“UIImage *(^)(void)”传递给“disptach_block_t”类型参数的不兼容块指针类型(又名“void (^)(void)”)

我知道块开头的^{表示无效,但是可以将其更改为返回UIImage吗?还是我从一开始就做错了?

我只是不知道如何将 PhotoListTableViewController 中的图像返回给 MapViewController。这应该怎么做?

4

1 回答 1

2

我正在做同样的任务。我在调用 tableview 控制器委托时切换了线程,而不是等到 table view 控制器获取图像,这对我有用。

在代码中:

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { // NSLog(@"Annotation selected, getting thumbnail");

dispatch_queue_t downloadQueue = dispatch_queue_create("flickr download", NULL);
dispatch_async(downloadQueue, ^{
    UIImage *image = [self.delegate thumbNailImageForAnnotation:view.annotation sender: self];

    dispatch_async(dispatch_get_main_queue(), ^{

        [(UIImageView *)view.leftCalloutAccessoryView setImage:image];

    });

});

}

于 2013-01-26T18:38:51.233 回答