マップの一部をキャプチャして、ポップオーバーの上部に表示される画像を作成することに興味があります。UIImageをキャプチャすると思いますが、ピンの座標からキャプチャできますか?
ありがとう
マップの一部をキャプチャして、ポップオーバーの上部に表示される画像を作成することに興味があります。UIImageをキャプチャすると思いますが、ピンの座標からキャプチャできますか?
ありがとう
あなたはこのようなことを試すことができます:
- (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
付近をキャプチャし、キャプチャした画像を画面の左上隅に表示します。もちろん、マップの中心をピンの座標に置き換え、領域の幅/高さを好きなように作成して、結果の画像をポップアップビューに配置することもできます。
これは単なるデモンストレーションです。サンプルアプリのボタンを使用して、マップを目的の場所にパンした後、キャプチャをトリガーしました。
私のコードは、キャプチャされた長方形を1:1のサイズ比で表示するだけです。もちろん、必要に応じて、キャプチャUIImage
したUIImageView
ものを好きなように拡大縮小することができます。画像を大きくすることができます。ただし、そうすると画像の鮮明さが失われます。このプロセスは、のスクリーンキャプチャを実行するだけUIView
です。マップデータには直接作用しないため、拡大すると(画像をより大きなサイズで表示する)、実際にズームインしたときのように画像が鮮明になりませんMKMapView
。
あなたiOS7
が使用することができますMKMapSnapshotter