Reputation: 8836
How to capture a designated regional screenshot on screen in Xcode?
For example, I hope to capture a region of (0,100,320,200) on screen and save to UIImage object?
Thanks
Upvotes: 0
Views: 3619
Reputation: 4789
Try the Below code for full screen screenshot (key window)
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
CGRect rect = [keyWindow bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[keyWindow.layer renderInContext:context];
UIImage *capturedScreen = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Try This code capture a UIView in native resolution
CGRect rect = [captureView bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[captureView.layer renderInContext:context];
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
This saves the UIImage in jpg format with 95% quality in the app's document folder if you need to do that.
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/capturedImage.jpg"]];
[UIImageJPEGRepresentation(capturedImage, 0.95) writeToFile:imagePath atomically:YES];
Source : How Do I Take a Screen Shot of a UIView?
Upvotes: 4