1

I've got a view controller with a button which takes a screenshot of the whole screen when pressed:

@implementation ViewController
-(IBAction)Screenshot {
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshotimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(screenshotimage, nil, nil, nil);
}
@end

The Problem : How should I get a screenshot of a specific part of the screen?

i.e. I need the screenshot to be a specific rectangle in the screen and not the whole screen

Any suggestions ?

Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Jay Chauhan
  • 99
  • 1
  • 9

2 Answers2

1

You have two choices:

  • You are saying [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; Instead, say [otherview.layer renderInContext:UIGraphicsGetCurrentContext()]; where otherview is a view you've put in place in advance, containing as subviews all the part of the screen you want to take a screenshot of.

  • Or, do it the way you are doing it, but crop (in code) the resulting screenshot image to the region that you want.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    This section of my book discusses how to make an image that is a cropped version of an existing image: http://www.apeth.com/iOSBook/ch15.html#_cgimage_drawing – matt May 11 '14 at 20:37
0

Create the image context with just the size of the rectangle you need and "move" around the origin of the coordinate system using CGContextTranslateCTM before you call renderInContext: to have the specific part of the screen drawn into that context.

iOSX
  • 1,270
  • 13
  • 18