Skip to content

Instantly share code, notes, and snippets.

@Kursulla
Last active December 17, 2015 18:49
Show Gist options
  • Save Kursulla/5656006 to your computer and use it in GitHub Desktop.
Save Kursulla/5656006 to your computer and use it in GitHub Desktop.
Create screenshot in iOS. [Objective-C,ios,xcode]
CGSize screenSize = [[UIScreen mainScreen] applicationFrame].size;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(nil, screenSize.width, screenSize.height, 8, 4*(int)screenSize.width, colorSpaceRef, kCGImageAlphaPremultipliedLast);
CGContextTranslateCTM(ctx, 0.0, screenSize.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
[(CALayer*)self.view.layer renderInContext:ctx];
CGImageRef cgImage = CGBitmapContextCreateImage(ctx);
UIImage *image = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
CGContextRelease(ctx);
//UIImage *image can be used for saving for file, or to be placed in some UIImageView or wathever...
[UIImageJPEGRepresentation(image, 1.0) writeToFile:@"screen.jpg" atomically:NO];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment