UIView screen capture using UIButton

I'm trying to make an application using a button that will take a screenshot of an object drawn on the deviceโ€™s screen and save it to the device gallery ...

How can i do this? any idea?

+4
source share
2 answers

Apple describes a way to do this here: 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; } 

And remember #import <QuartzCore/QuartzCore.h>

+3
source
  UIGraphicsBeginImageContext(self.view.bounds.size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *screenShotimage = UIGraphicsGetImageFromCurrentImageContext(); UIImageWriteToSavedPhotosAlbum(screenShotimage, nil, nil, nil); UIGraphicsEndImageContext(); 

This code will display a screenshot and save the image in the photo gallery.

+4
source

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


All Articles