未添加的MKMapView renderInContext问题

时间:2013-08-01 02:39:56

标签: iphone ios objective-c mkmapview rendering

我正在尝试从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];

以下是发生的事情的图像:

problem

0 个答案:

没有答案