1

我正在尝试从 MKMapView 中获取图像,以将其作为“缩略图”图像添加到 UITableViewCell。问题是图像呈现为纯色而不是正确的地图。出于测试目的,我已将 MKMapView 直接添加到我的视图中,并且它可以正确加载。

下面是来自 cellForRowAtIndexPath 的一些代码:

//create correct map thumbnail
    MKMapView *mapForThumbnail = [[MKMapView alloc] init];
    mapForThumbnail.frame = CGRectMake(0, 0, 100, 100);
    mapForThumbnail.delegate = self;

    CLLocationCoordinate2D coords[coordinates.count];
    for(int i = 0; i < coordinates.count; i++){
        CLLocation *currentLocationCoords = [coordinates objectAtIndex:i];
        coords[i] = CLLocationCoordinate2DMake(currentLocationCoords.coordinate.latitude, currentLocationCoords.coordinate.longitude);

    }//end for

    MKPolyline *polylineForMapThumbnail = [MKPolyline polylineWithCoordinates:coords count:coordinates.count];
    [mapForThumbnail addOverlay:polylineForMapThumbnail];
    [mapForThumbnail setRegion:MKCoordinateRegionForMapRect([polylineForMapThumbnail boundingMapRect])];

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(100, 100), NO, 0.0);
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    [mapForThumbnail.layer renderInContext:currentContext];
    UIImage *mapImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
 cell.imageView.image = mapImage;
    cell.detailTextLabel.text = stringForDetailLabel;
    cell.detailTextLabel.font = [UIFont fontWithName:@"Verdana" size:12.0f];
    cell.detailTextLabel.textColor = [UIColor darkGrayColor];

这是正在发生的事情的图像:

问题

4

0 回答 0