Take a screen shot of iOs 5 programmatically

I am developing an iPhone iOS 5, I would like to take a screenshot programmatically when the user clicks the view button in the bulletin board. I tried a lot of codes, but they are for older versions of iOS. How can I do this in iOS 5. Thanks!

+4
source share
3 answers
- (void)drawRect:(CGRect)rect { UIGraphicsBeginImageContext(self.bounds.size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } 
+12
source

Well, there are several ways to capture the iPhone screen programmatically

Of these 5 methods, I found the first option, very convenient for copying and applying the compression level, since the 1st method gives an image with true pixel data.

+1
source

Since this is an old question, I put the code here, hope to help someone. (The code is a copy of Apple: http://developer.apple.com/library/ios/#qa/qa1703/_index.html )

 - (UIImage*)screenshot { // Create a graphics context with the target size // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext CGSize imageSize = [[UIScreen mainScreen] bounds].size; if (NULL != UIGraphicsBeginImageContextWithOptions) UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); else UIGraphicsBeginImageContext(imageSize); CGContextRef context = UIGraphicsGetCurrentContext(); // Iterate over every window from back to front for (UIWindow *window in [[UIApplication sharedApplication] windows]) { if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) { // -renderInContext: renders in the coordinate space of the layer, // so we must first apply the layer geometry to the graphics context CGContextSaveGState(context); // Center the context around the window anchor point CGContextTranslateCTM(context, [window center].x, [window center].y); // Apply the window transform about the anchor point CGContextConcatCTM(context, [window transform]); // Offset by the portion of the bounds left of and above the anchor point CGContextTranslateCTM(context, -[window bounds].size.width * [[window layer] anchorPoint].x, -[window bounds].size.height * [[window layer] anchorPoint].y); // Render the layer hierarchy to the current context [[window layer] renderInContext:context]; // Restore the context CGContextRestoreGState(context); } } // Retrieve the screenshot image UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } 
0
source

Source: https://habr.com/ru/post/1383297/


All Articles