References
http://iphoneincubator.com/blog/tag/uigraphicsbeginimagecontext
Source codes:
UIGraphicsBeginImageContext(imageView.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
NOTES:
1. UIGraphicsBeginImageContext
Creates a bitmap-based graphics context and makes it the current context.
void UIGraphicsBeginImageContext ( CGSize size );Parameters
size
The size of the new bitmap context. This represents the size of the image returned by the
UIGraphicsGetImageFromCurrentImageContext
function.Discussion
You use this function to configure the drawing environment for rendering into a bitmap. This drawing environment uses the premultiplied RGBA format to store the resulting bitmap data. The environment also uses the default coordinate system for UIKit views, where the origin is in the upper-left corner and the positive axes extend down and to the right of the origin. The drawing environment is pushed onto the graphics context stack immediately.
While the context created by this function is the current context, you can call the
UIGraphicsGetImageFromCurrentImageContext
function to retrieve an image object based on the current contents of the context. When you are done modifying the context, you must call theUIGraphicsEndImageContext
function to clean up the bitmap drawing environment and remove the graphics context from the top of the context stack. You should not use theUIGraphicsPopContext
function to remove this type of context from the stack.In most other respects, the graphics context created by this function behaves like any other graphics context. You can change the context by pushing and popping other graphics contexts. You can also get the bitmap context using the
UIGraphicsGetCurrentContext
function.
2. Fill rect
[[UIColor redColor] set];
UIRectFill(CGRectMake(10,10, 200, 100));
3. Draw Text
NSString* greeting = @"Tom, how are you now ";
[greeting drawAtPoint:CGPointMake(205, 0) withFont:[UIFont fontWithName:@"Helvetica" size:36.0]];