简体   繁体   English

在后台加载MKMapView并从中创建UIImage(iPhone和iPad)

[英]Load MKMapView in background and create UIImage from it (iPhone & iPad)

I have a situation whereby I need a way of loading an MKMapView with several overlays. 我有一种情况,我需要一种方法来加载MKMapView与几个叠加。 This map view shouldn't be shown onscreen, the only reason I require it to load is in order to create an image from the map for use elsewhere. 此地图视图不应显示在屏幕上,我需要加载的唯一原因是为了从地图创建图像以供其他地方使用。

I have had a look around online but I haven't had any luck with finding a solution to the problem. 我在网上看了看,但我没有找到解决问题的办法。 Please can someone help me out? 请有人帮帮我吗?

Here is what I've tried so far (with no success) 这是我到目前为止尝试过的(没有成功)

// Load the map view
Logs_Map_ViewController *mapViewController = [[Map_ViewController alloc] initWithNibName:@"Map_ViewController" bundle:nil];
mapViewController.GPSCoordinatesToPlot = [someDictionary objectForKey:kGPSCoords];

// Take a picture of the map
UIImage *mapImage = [mapViewController convertMapToImage];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(mapImage)];
NSString *base64String = [imageData base64EncodedString];

NOTE: It's really important that the app waits for the map to fully load (so that the image is created properly). 注意:应用程序等待地图完全加载(以便正确创建图像)非常重要。

This may or may not be helpful but this is what I do in my situation. 这可能有用也可能没用,但这就是我在我的情况下所做的。

I have a tableview for an venue/business with 7 table cells. 我有一个有7个桌子单元的场地/商业的桌面视图。 One of the table cells contains a map with pindrop. 其中一个表格单元格包含一个带有pindrop的地图。 When the user views an establishment for the very first time, i run a quick conditional to see if the mapview has been cached (or rather, saved as image). 当用户第一次查看企业时,我运行快速条件以查看mapview是否已被缓存(或者更确切地说,保存为图像)。 if the image does not exist, i load a fresh mapview into the cell and add a single pindrop annotation. 如果图像不存在,我将新的mapview加载到单元格中并添加单个pindrop注释。 After both the mapview has loaded and the annotation has dropped, i save the view layer to image, then remove the mapview (and release it since we needed to keep the delegate alive) from the cell and replace with the fresh new image. 加载了mapview并删除了注释后,我将视图层保存到图像,然后从单元格中删除mapview(并释放它,因为我们需要让代理保持活动状态)并替换为新的新图像。 I suggest spawing a background thread to save the image/insert into cell. 我建议使用后台线程来保存图像/插入到单元格中。 This way the table view doesn't choke up. 这样表格视图就不会窒息。

And then finally, the next time the user returns to that same venue/business, the mapview image exists and i load the image. 最后,下次用户返回同一场地/业务时,地图视图图像存在,我加载图像。

I know it's important for the map to fully load, otherwise your image saves with gray where the map did not load. 我知道地图完全加载很重要,否则你的图像会保存灰色,而地图没有加载。 if you're dropping a pin drop, you need to make sure your image saving method fires after both the map finished loading (mapViewDidFinishLoadingMap:) and your annotation has dropped (mapView:didAddAnnotationViews:). 如果您正在丢弃引脚丢弃,则需要确保在映射完成加载(mapViewDidFinishLoadingMap :)并且您的注释已删除(mapView:didAddAnnotationViews :)之后,图像保存方法将触发。 Only after both have finished should you fire method to save image. 只有在两者完成后才应该开火方法来保存图像。 i suggest using a delayed selector. 我建议使用延迟选择器。 I delay it by 1 second. 我推迟了1秒。

This same concept can surely be applied to other situations. 同样的概念肯定可以应用于其他情况。 Bottom line is, as strings42 states, you're going to have to add a visible mapview to your view in order to capture an image. 底线是,正如strings42所述,您将不得不在视图中添加可见的mapview以捕获图像。

You should be able to implement mapViewDidFinishLoadingMap in the MKMapViewDelegate protocol and store your image there, I would think. 您应该能够在MKMapViewDelegate协议中实现mapViewDidFinishLoadingMap并将图像存储在那里,我想。 That should solve the problem of needing to wait until the map is loaded to store the image. 这应该解决需要等到加载地图以存储图像的问题。

If you're seeing other issues as well, please provide more detail on those, as your question is not clear on the specific issue(s) that you're asking about. 如果您还看到其他问题,请提供有关这些问题的更多详细信息,因为您的问题并不清楚您所询问的具体问题。

The method below will always be called as the delegate of mapView, 下面的方法将始终作为mapView的委托调用,

mapViewDidFinishRenderingMap:fullyRendered:

so, you can get map image from mapView here 所以,你可以在这里从mapView获取地图图像

- (UIImage *)renderToImage:(MKMapView *)mapView
{
  UIGraphicsBeginImageContext(mapView.bounds.size);
  [mapView.layer renderInContext:UIGraphicsGetCurrentContext()];
  UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return image;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM