Mark the MKMapView part for the image

I am interested in capturing part of the map to create the image that you see at the top of the popover. I guess I will capture UIImage, can you do this starting at the coordinates of the pin?

thanks

Apple maps popover

+6
source share
2 answers

In iOS7 you can use MKMapSnapshotter

+4
source

You can try something like this:

 - (UIImage*) imageFromView:(UIView*)view rect:(CGRect)rect { // capture the full view in an image UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage* viewImg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // crop down to just our desired rectangle, accounting for Retina scale CGFloat scale = [[UIScreen mainScreen] scale]; CGRect scaledRect = CGRectMake(scale * rect.origin.x, scale * rect.origin.y, scale * rect.size.width, scale * rect.size.height); CGImageRef resultImgRef = CGImageCreateWithImageInRect([viewImg CGImage], scaledRect); UIImage* result = [UIImage imageWithCGImage: resultImgRef scale: 1.0f / scale orientation: UIImageOrientationUp]; CGImageRelease(resultImgRef); return result; } - (IBAction)onButtonTapped:(id)sender { // just pick the map center as the location to capture. could be anything. CLLocationCoordinate2D center = self.map.centerCoordinate; // convert geo coordinates to screen (view) coordinates CGPoint point = [self.map convertCoordinate:center toPointToView: self.map]; // make this span whatever you want - I use 120x80 points float width = 120.0; float height = 80.0; // here the frame in which we'll capture the underlying map CGRect frame = CGRectMake(point.x - width / 2.0, point.y - height / 2.0, width, height); // just show the captured image in a UIImageView overlay // in the top, left corner, with 1:1 scale UIImageView* overlay = [[UIImageView alloc] initWithImage: [self imageFromView: self.map rect: frame]]; frame.origin = CGPointZero; overlay.frame = frame; [self.view addSubview: overlay]; } 

The imageFromView:rect: method simply captures the specified rectangular area within a given view and creates a UIImage .

The onButtonTapped method uses the first method to capture the area around the center of my map and display the captured image in the upper left corner of the screen. Of course, you can replace the center map with the coordinate of your pin, make the width and height of the area that you like, and put the resulting image in your pop-up window.

This is just a demonstration. I used the button in my example application, just to start capturing, after panning the map where I would like to do this.

results

enter image description here

Limitations

My code just displays a captured rectangle with a 1: 1 ratio. Of course, if you want, you can put the captured UIImage into a UIImageView , which scales it as you like. You can make the image bigger. But, if you do, you will lose sharpness. This process simply performs a UIView screen UIView . It does not work directly with map data, so when you explode it (display the image in a larger size), the image will not be sharpened, as with the actual zoom on MKMapView .

+5
source

All Articles