I am performing some CG drawing operations into a CGContext that I created for an MKMapOverlay开发者_StackOverflow中文版View. After drawing into my context, I create an image and paste it into the context that MapKit provided.
- (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);
}
Is there a way to simply copy myContext
into context
without having to create an image?
I realize that some of you will say "why not just draw directly into the context that MapKit provides". Unfortunately, we're experiencing a drawing glitch when rendering into context
directly. Apple is currently investigating this issue for us, but in the meantime we need to get a workaround in place. This workaround I presented above is my "best" shot, but it is a bit on the slow side.
P.S. I have started a bounty since I'm looking for an answer here too. Specifically I'm targeting OS X. So the answer should work there. The OP was looking for an answer on iOS.
You could use a CGLayerRef. The layer ref is like sub-context that you do a bunch of drawing into, and then flatten down into the original context when you finish drawing the layer's worth of content.
It's typically used to get shared alpha or shadows across many drawing calls rather than each individual call.
I don't know if this will still workaround whatever bug you're encountering, or whether the performance is better or worse than the two context approach. I'd need to know more about your goals and requirements. For example, do you you want to avoid using two context to avoid a second copy, or because you don't want to pay the memory for the second image?
the actual physical copy of the bits occur only if the underlying data in the bitmap graphics context is modified when you use CGBitmapContextCreateImage, so there is not much performance loss when you create an image from the CGBitmapContext.
精彩评论