1

我已经创建了与 WWDC 上显示的类似代码,用于在快照上显示 pin,但 pin 图像不显示:

MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
    options.region = self.mapView.region;
    options.scale = 2;
    options.size = self.mapView.frame.size;
    MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
    [snapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error)
     {
         MKAnnotationView *pin = [[MKAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:@""];
         UIImage *image;
         UIImage *finalImage;
         image = snapshot.image;
         NSLog(@"%f", image.size.height);
         UIImage *pinImage = pin.image;
         CGPoint pinPoint = [snapshot pointForCoordinate:CLLocationCoordinate2DMake(self.longtitude, self.latitude)];
         CGPoint pinCenterOffset = pin.centerOffset;
         pinPoint.x -= pin.bounds.size.width / 2.0;
         pinPoint.y -= pin.bounds.size.height / 2.0;
         pinPoint.x += pinCenterOffset.x;
         pinPoint.y += pinCenterOffset.y;


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

         finalImage = UIGraphicsGetImageFromCurrentImageContext();
         UIGraphicsEndImageContext();

         NSData *data = UIImageJPEGRepresentation(finalImage, 0.95f);
         NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
         NSString *path = [pathArray objectAtIndex:0];
         NSLog(@"%@", path);
         NSString *fileWithPath = [path stringByAppendingPathComponent:@"test.jpeg"];
         [data writeToFile:fileWithPath atomically:YES];
     }];

仅显示地图快照,不显示图钉图像。

4

1 回答 1

3

如果您希望出现默认图钉图像,则需要创建一个MKPinAnnotationView而不是普通MKAnnotationView图(它没有默认图像——默认情况下它是空白的)。


另外,请注意,纬度和经度参数在此行中是向后的:

CGPoint pinPoint = [snapshot pointForCoordinate:CLLocationCoordinate2DMake(
                       self.longtitude, self.latitude)];

其中CLLocationCoordinate2DMake,纬度应该是第一个参数,经度应该是第二个参数。

于 2014-02-03T04:21:11.070 回答