How to take a screenshot programmatically in iOS?
Solution 1
use following code to take screen shot
-(UIImage *) screenshot
{
CGRect rect;
rect=CGRectMake(0, 0, 320, 480);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context=UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Solution 2
-(UIImage *) screenshot
{
CGImageRef UIGetScreenImage(void);
CGImageRef screen = UIGetScreenImage();
UIImage* screenImage = [UIImage imageWithCGImage:screen];
CGImageRelease(screen); // you need to call this.
return screenImage;
}
Solution 3
Well, there are few ways of capturing the iPhone screen programmatically
Using UIKIT http://developer.apple.com/library/ios/#qa/qa1703/_index.html
Using AVFoundation framework http://developer.apple.com/library/ios/#qa/qa1702/_index.html
Using OpenGL ES http://developer.apple.com/library/ios/#qa/qa1704/_index.html
Starting from iOS 7 you can also use Why does my programmatically created screenshot look so bad on iOS 7?
-
Using UIWindow
CGRect screenRect = [[UIScreen mainScreen] bounds]; UIGraphicsBeginImageContext(screenRect.size); CGContextRef ctx = UIGraphicsGetCurrentContext(); [[UIColor blackColor] set]; CGContextFillRect(ctx, screenRect); [self.window.layer renderInContext:ctx]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
-
Using UIView
UIGraphicsBeginImageContext(self.view.frame.size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viImage=UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
Out of these 6 options, I found the first option very convenient for copy-pasting and for applying level of compression as 1st method gives the image with true pixel data. I also like option 4, as the API comes with SDK.
Solution 4
You get result Uiimage without lose quality of image
- (UIImage *)captureView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
Solution 5
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *data = [UIImagePNGRepresentation(image) retain];
UImage *screenShot = [UIImage imageWithData:data];
coodi8
Updated on July 23, 2022Comments
-
coodi8 almost 2 years
I have a
UIView
that uses bothUIKit
control andOpenGL
. I'd like to get a screenshot of that view programatically.If I use
UIGraphicsGetImageFromCurrentImageContext()
, theOpenGL
content is blank; If I use theglReadPixels(...)
method, the UIKit content is blank;I'm confused as how to take a complete screenshot. Thank you!
-
coodi8 over 11 yearsit doesn't take effects in an OpenGL page
-
coodi8 over 11 yearsit only take effects in UIKit condition, for OpenGL page, only get blank image
-
coodi8 over 11 yearsit only take effects in UIKit condition, for OpenGL page, only get blank image
-
coodi8 over 11 yearsI'm developing a screenshot control, I don't know whether the view is implemented by UIKit or OpenGL, or mixed. If I can get the info that the view is implemented by what, the problem will be solved.
-
Felix K. over 11 years@coodi8 I think this could be a problem. Won't be easy because OpenGL renders directly on the graphic chip. UI-Graphics are not rendered on the graphics chip. ( AFAIK )
-
slycrel over 10 yearsThanks for this, it seems that screens with sub-view controllers don't auto-add child view controller layers. But using the window seems to work for a full-screen image. Thanks!
-
ZYiOS over 10 yearsCan someone explain how it works? Also need to fix the orientation
-
Matteo Gobbi almost 10 yearsThis code is just for take UIKit stuff...The question is more complex and well explained: he need to take also OpenGL stuff.
-
Julian over 8 yearsdefinitely not the way to go this is why