MKAnnotationView图像不显示在结果快照图像iOS 7上

时间:2014-02-02 15:36:59

标签: iphone ios7 mkmapview mkannotationview mkmapsnapshotter

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

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];
     }];

仅显示地图快照而没有图钉。

1 个答案:

答案 0 :(得分:3)

如果您希望显示默认图片图片,则需要创建 MKPinAnnotationView 而不是普通MKAnnotationView(没有默认图片 - 它是默认为空白。)


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

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

CLLocationCoordinate2DMake中,纬度应为第一个参数,经度为第二个