Search code examples
iosios5quartz-graphicsquartz-2d

how to draw with UIGraphicsBeginImageContext()


Im trying to generate an image, and have found UIGraphicsBeginImageContext() in the Apple docs which looks perfect. Ive been looking at some Quartz tutorials and in each one they use a custom view to do their drawing which doesn't seem necessary in this case, but i dont know enough to be sure. Whats the best way to do my drawing using UIGraphicsBeginImageContext()?


Solution

  • Well, you probably want to use UIGraphicsBeginImageContextWithOptions() and scale=0.0 to get resolution independence, but yes, once you call the function the frameworks will have set up a normal graphics context you can use as in the tutorials. You can get at it with UIGraphicsGetCurrentContext().

    When you have finished drawing you will likely want to use UIGraphicsGetImageFromCurrentImageContext()to actually capture what you have drawn.

    And don't forget to call UIGraphicsEndImageContext() when finished.