在给定的纬度和经度处从 MKMapView 生成 UIImage

发布于 2024-10-29 21:50:26 字数 229 浏览 2 评论 0原文

我有一个使用 MKMapView 来显示地图的应用程序。我还有一个 UITableView,它在每行的左侧显示地图的一个小片段图像。它看起来像这样:

在此处输入图像描述

我希望能够在 MKMapView 的左侧生成该图像。尺寸为 40x40。我知道给定的纬度和经度是我想要获取图像的特定位置的中心。我该怎么做?

I have an app that uses MKMapView to display a map. I also have a UITableView in which it displays a small snippet image of the map to the left of each rows. It looks something like this:

enter image description here

I want to be able to generate that image to the left from my MKMapView. The size is 40x40. I know the given latitude and longitude as the center of a particular location where I want to get the image. How do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你在看孤独的风景 2024-11-05 21:50:26

要获取视图的快照:

-(UIImage *)pictureForView:(UIView*)view
{
    UIGraphicsBeginImageContext(view.frame.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    [view.layer renderInContext:ctx];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    return image;
}

这是整个视图 - 您需要从中雕刻出您需要的部分。或者也许只是将地图视图集中在您需要的位置上并进行最大缩放。然后,当将整个视图的图片调整为缩略图大小时,可能就足够了。

要将地图图像裁剪到所需位置:

UIImage* mapImage = [self pictureForView:self.mapView];
MKCoordinateRegion mapRegion = self.mapView.region;
//pointOfInterest is assumed to be on the map view, e.g. get the coordinate of the pin
CLLocationCoordinate2D pointOfInterest = ????;

double mapLatFrom = mapRegion.center.latitude - mapRegion.span.latitudeDelta/2;
double mapLonFrom = mapRegion.center.longitude - mapRegion.span.longitudeDelta/2;

double cropX = ((pointOfInterest.latitude - mapLatFrom)/mapRegion.span.latitudeDelta)*mapView.frame.size.width - self.imageView.frame.size.width /2;
double cropY = ((pointOfInterest.longitude - mapLonFrom)/mapRegion.span.longitudeDelta)*mapView.frame.size.height - self.imageView.frame.size.height /2;

CGRect cropRect = CGRectMake(cropX, cropY, self.imageView.frame.size.width, self.imageView.frame.size.height);
CGImageRef imageRef = CGImageCreateWithImageInRect([mapImage CGImage], cropRect);

self.imageView.image = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

To get a snapshot of a view:

-(UIImage *)pictureForView:(UIView*)view
{
    UIGraphicsBeginImageContext(view.frame.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    [view.layer renderInContext:ctx];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    return image;
}

That's for the whole view - you'll need to carve the piece you need out of it. Or maybe just center the map view on the location you need and do the maximum zoom. Then the picture of the whole view, when resized to a thumbnail size, might be just good enough.

To crop the map image to the desired location:

UIImage* mapImage = [self pictureForView:self.mapView];
MKCoordinateRegion mapRegion = self.mapView.region;
//pointOfInterest is assumed to be on the map view, e.g. get the coordinate of the pin
CLLocationCoordinate2D pointOfInterest = ????;

double mapLatFrom = mapRegion.center.latitude - mapRegion.span.latitudeDelta/2;
double mapLonFrom = mapRegion.center.longitude - mapRegion.span.longitudeDelta/2;

double cropX = ((pointOfInterest.latitude - mapLatFrom)/mapRegion.span.latitudeDelta)*mapView.frame.size.width - self.imageView.frame.size.width /2;
double cropY = ((pointOfInterest.longitude - mapLonFrom)/mapRegion.span.longitudeDelta)*mapView.frame.size.height - self.imageView.frame.size.height /2;

CGRect cropRect = CGRectMake(cropX, cropY, self.imageView.frame.size.width, self.imageView.frame.size.height);
CGImageRef imageRef = CGImageCreateWithImageInRect([mapImage CGImage], cropRect);

self.imageView.image = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文