Question

I`ve created similar code as was shown on WWDC for displaying pin on snapshots, but pin image is not displayed:

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

Only snapshot of map is displayed without pin image.

Était-ce utile?

La solution

If you're expecting the default pin image to appear, you need to create an MKPinAnnotationView instead of the plain MKAnnotationView (which has no default image -- it's blank by default).


Also, please note that the latitude and longitude parameters are backwards in this line:

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

In CLLocationCoordinate2DMake, latitude should be the first parameter and longitude the second.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top