我需要能够以编程方式绘制图像,并保存该图像供以后使用.比如说,在图像上的特定x和y坐标上画一条线,保存图像,并将其显示在一个简单的视图控制器上.我将如何在Swift中执行此操作?(最好是Swift 2,我还在开发中并且没有将我的mac更新到Sierra)
更新:可能与将UIImage转换为CGLayer,绘制它,然后将其转换回UIImage有关.
我认为iOS 7 MKMapSnapshotter是一种拍摄快照的简单方法MKMapView,其好处是可以在不将地图加载到视图中的情况下完成.即使添加引脚和覆盖图似乎更多的工作(因为需要核心图形).WWDC视频提供了一个非常好的例子来创建一个MKMapSnapshotter添加MKAnnotationView.
然而,对于没有很多核心图形经验的人来说,如何创建一个MKMapSnapshotter来自的人并不是很明显MKPolylineRenderer.
我试过这样做,但路径不准确.它准确地绘制了大约10%的线,但随后直接绘制了剩下的路径.
这是我的代码:
MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:snapshotOptions];
[snapshotter startWithQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
completionHandler:^(MKMapSnapshot *snapshot, NSError *error)
{
if (error == nil)
{
UIImage *mapSnapshot = [snapshot image];
UIGraphicsBeginImageContextWithOptions(mapSnapshot.size,
YES,
mapSnapshot.scale);
[mapSnapshot drawAtPoint:CGPointMake(0.0f, 0.0f)];
CGContextRef context = UIGraphicsGetCurrentContext();
//Draw the points from the MKPolylineRenderer in core graphics for mapsnapshotter...
MKPolylineRenderer *overlay = (MKPolylineRenderer *)[self.mapView rendererForOverlay:[_mapView.overlays lastObject]];
if (overlay.path)
{
CGFloat zoomScale = 3.0;
[overlay applyStrokePropertiesToContext:context
atZoomScale:zoomScale];
CGContextAddPath(context, …Run Code Online (Sandbox Code Playgroud)