我有兴趣捕捉地图的一部分,以创建您在弹出窗口顶部看到的图像。我想我会捕获一个UIImage,你可以从pin的坐标开始这样做吗?
由于
答案 0 :(得分:5)
您可以尝试这样的事情:
- (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's 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's 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];
}
imageFromView:rect:
方法只捕获给定视图中的给定矩形区域,并生成UIImage
。
onButtonTapped
方法使用第一种方法捕获地图 center 周围的区域,并在屏幕的左上角显示捕获的图像。当然,您可以使用引脚的坐标替换地图 center ,根据需要制作区域宽度/高度,并将生成的图像放入弹出视图中。
这只是一个示范。我在我的示例应用程序中使用了一个按钮,只是为了触发捕获,然后将地图平移到我想要的位置。
我的代码只是以1:1的比例显示捕获的矩形。当然,如果您愿意,可以将捕获的UIImage
放入UIImageView
,然后根据您的喜好进行缩放。你可以让图像更大。但是,如果你这样做,你将失去图像清晰度。此过程仅执行UIView
的屏幕截图。它不能直接对地图数据起作用,因此当您将其向上(以更大的尺寸显示图像)时,图像不会像实际放大MKMapView
时那样锐化。
答案 1 :(得分:4)
在iOS7
上,您可以使用MKMapSnapshotter