从 MKMapView 制作 UIImage
我想从 MKMapView
创建一个 UIImage
。我的地图在视图中正确显示,但是生成的 UIImage 只是灰色图像。这是相关的片段。
UIGraphicsBeginImageContext(mapView.bounds.size);
[mapView.layer renderInContext:UIGraphicsGetCurrentContext()];
mapImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
有人知道如何使用 MapKit 制作 UIImage
吗?
I want to create a UIImage
from a MKMapView
. My map is correctly displayed in the view, however the UIImage
produced is just a gray image. Here's the relevant snippet.
UIGraphicsBeginImageContext(mapView.bounds.size);
[mapView.layer renderInContext:UIGraphicsGetCurrentContext()];
mapImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Anyone know how to make a UIImage
using MapKit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我使用的代码与 ios sdk 4.1 测试的相同并且工作正常。因此,当地图已经显示给用户并且用户按下按钮时,将调用此操作:
这是作为 UIView 扩展实现的包装函数:
所以,问题不在于该代码部分。
I am using the same code that is tested with ios sdk 4.1 and works fine. So, when map is already displayed to user and user press the button this action will be called:
and here is the wrapper function realized as UIView extension:
So, the problem is not in that code part.
在 iOS7 上,MapKit 上有一个新的 API,称为 MKMapSnapshotter。因此,您实际上不需要创建地图视图、加载图块并创建捕获自己的图形上下文。
请查看 https://developer .apple.com/library/ios/documentation/MapKit/Reference/MKMapSnapshotter_class/Reference/Reference.html
On iOS7, there is a new API on MapKit for this called MKMapSnapshotter. So you actually don't need to create a mapview, load the tiles and create a graphic context capturing yourself.
Take a look into it at https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapSnapshotter_class/Reference/Reference.html
这是视网膜显示的改进功能:
Here is the improved function for retina display:
嘿洛伦。地图视图中有多个图层。我认为第一个是地图,第二个是谷歌图层。他们可能在 3.1 之后更改了 Mapkit 中的某些内容。您可以尝试
您也可以尝试
希望这会有所帮助。
Hey Loren. There are multiple layers in the mapView. I think the first one is the map and the second on is the google layer. They might have changed something in the mapkit after 3.1. You can try
You can also try
Hope this helps.
请注意,mapView 可能无法完成加载,因此图像可能是灰色的。
as
不会总是被调用,你应该在
so 中获取 UIImage,代码就像这样
Note that, mapView may not finish load so image may be grey.
as
will not always be called, you should get UIImage in
so, the code just like this
如果您在初始化地图后立即调用此方法(也许在 viewDidLoad 中?),您可能会得到灰色图像,因为地图尚未完成绘制。
尝试:
<强>编辑:
在模拟器上,使用performSelector,可以实现零延迟。在设备上,需要较长的延迟(大约5秒)。
但是,如果您添加注释(并在 didAddAnnotationViews 方法中捕获),它会立即在模拟器和设备上运行。
If you are calling this immediately after initializing the map (maybe in viewDidLoad?), you could get a gray image as the map is not finished drawing yet.
Try:
Edit:
On the simulator, using performSelector, a zero delay works. On the device, a longer delay is required (about 5 seconds).
However, if you add annotations (and you capture in the didAddAnnotationViews method), it works immediately on both the simulator and device.