This post deals with taking screenshot of the iPhone screen with the help of coding.
Remember, when we say screen shot of the iPhone screen, we mean screenshot of any view which is visible on the iPhone screen. Also, a view is captured with all its subviews that are visible on screen at the time of capturing.
Add the following code at the place where you want to take screenshot of the view.
UIGraphicsBeginImageContext(view.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
A Context, more accurately known as Graphics Context, is the drawing destination for your objects. A context can be a Bitmap image, a UIView layer, a PDF file or a Printer.
In the above code, view is the UIView object which has to be captured.
UIGraphicsBeginImageContext(view.frame.size) creates a bitmap image context and set it as the current context. It takes size as parameter. The image context made is of the same size as the size passed in as parameter to this function. Since, here we want the image context to be of same size as of view to be captured so we pass the size of view as parameter.
UIGraphicsGetCurrentContext() returns the current context for drawing. Since we already set the bitmap context as the current context so it will return the same bitmap context created above. Had the bitmap context not been set , all the drawings would have taken place on UIView layer context(which is the default current context).
[view.layer renderInContext:context] draws all the objects, on or above the view’s layer, to the context passed in as parameter.
UIGraphicsGetImageFromCurrentImageContext() returns an image based on the contents of the current bitmap-based graphics context.
No comments:
Post a Comment