具有MKPolylineRenderer问题的MKMapSnapshotter

时间:2014-06-25 18:09:13

标签: ios xcode ios7 mkmapview overlays

我尝试实现这个答案:https://stackoverflow.com/a/22716610,解决了在iOS7中为mkmapsnapshotter添加叠加层的问题(不能做renderInContext方法)。我这样做了,如下所示,但返回的图像只有没有叠加的地图。原谅我,我对此很陌生。感谢。

-(void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered
{
if (mapView.tag == 100) {

    MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
    options.region = mapView.region;
    options.size = mapView.frame.size;
    options.scale = [[UIScreen mainScreen] scale];

    MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
    [snapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
        if (error) {
            NSLog(@"[Error] %@", error);
            return;
        }

        UIImage *image = snapshot.image;

        UIGraphicsBeginImageContextWithOptions(image.size, YES, image.scale);
        {
            [image drawAtPoint:CGPointMake(0, 0)];

            CGContextRef context = UIGraphicsGetCurrentContext();

            CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
            CGContextSetLineWidth(context,5.0f);
            CGContextBeginPath(context);
            bool first = YES;
            NSArray *overlays = mapView.overlays;
            for (id <MKOverlay> overlay in overlays) {
                CGPoint point = [snapshot pointForCoordinate:overlay.coordinate];

                if(first)
                {
                    first = NO;
                    CGContextMoveToPoint(context,point.x, point.y);
                }
                else{
                    CGContextAddLineToPoint(context,point.x, point.y);

                }
            }
            UIImage *compositeImage = UIGraphicsGetImageFromCurrentImageContext();
            NSData *data = UIImagePNGRepresentation(compositeImage);
            placeToSave = data;
            NSLog(@"MapView Snapshot Saved.");

//show image for debugging
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 200, 320, 320)];
            imageView.image = compositeImage;
            [self.view addSubview:imageView];
        }
        UIGraphicsEndImageContext();
    }];

    [mapView setHidden:YES];
}
}

0 个答案:

没有答案