我正在为CGMapOverlayView创建的CGContext执行一些CG绘图操作 . 在绘制到我的上下文后,我创建了一个图像并将其粘贴到MapKit提供的上下文中 .
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
CGColorSpaceRef colorRef = CGColorSpaceCreateDeviceRGB();
CGContextRef myContext = CGBitmapContextCreate(NULL, kTileSize, kTileSize, 8, 0, colorRef, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorRef);
CGContextSetAllowsAntialiasing(myContext, TRUE);
//...cut out drawing operations...
CGImageRef image = CGBitmapContextCreateImage(myContext);
CGContextDrawImage(context, [self rectForMapRect:mapRect], image);
CGImageRelease(image);
CGContextRelease(myContext);
}
有没有办法简单地将 myContext
复制到 context
而无需创建图像?
我意识到有些人会说"why not just draw directly into the context that MapKit provides" . 不幸的是,当我们直接渲染到 context
时,我们遇到了绘图故障 . Apple目前正在为我们调查这个问题,但与此同时我们需要找到一个解决方法 . 我上面提到的这个解决方法是我的"best"镜头,但它有点慢 .
P.S. 自从我'm looking for an answer here too. Specifically I' m以OS X为目标以来,我已经开始获得赏金 . 所以答案应该在那里工作 . OP正在寻找iOS上的答案 .
1 回答
你可以使用CGLayerRef . 图层引用类似于子上下文,您可以在绘制图层的内容时完成一系列绘图,然后展平到原始上下文中 .
它通常用于在许多绘图调用中获取共享的alpha或阴影,而不是每个单独的调用 .
我不知道这是否仍然可以解决您遇到的任何错误,或者性能是否比两种上下文方法更好或更差 . 我需要更多地了解您的目标和要求 . 例如,您是否要避免使用两个上下文来避免第二个副本,或者因为您不想为第二个图像支付内存?