13

I am taking a screenshot in my application. I am able to take the screenshot.

Now I want to take the screenshot by specifying the x and y coordinate. Is that possible?

UIGraphicsBeginImageContext( self.view.bounds.size );
[self.view.layer renderInContext:UIGraphicsGetCurrentContext(  )];
UIImage* aImage = UIGraphicsGetImageFromCurrentImageContext(  );
user3708642
  • 124
  • 10
user198725878
  • 6,266
  • 18
  • 77
  • 135

5 Answers5

19
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(c, 0, -40);    // <-- shift everything up by 40px when drawing.
[self.view.layer renderInContext:c];
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
pixelfreak
  • 17,714
  • 12
  • 90
  • 109
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • @barbgal: Use CGContextScaleCTM to resize. Also, you may need a smaller context (change the argument to UIGraphicsBeginImageContext). – kennytm Nov 16 '10 at 13:31
  • 2
    @Barbgal: No. `CGSize size=self.view.bounds.size; UIGraphicsBeginImageContext(CGSizeMake(size.width,size.height-40)); CGContextRef c = UIGraphicsGetCurrentContext();` – kennytm Nov 16 '10 at 13:40
2

If you're using a newer retina display device, your code should factor in the resolution by using UIGraphicsBeginImageContextWithOptions instead of UIGraphicsBeginImageContext:

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size,YES,2.0);
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(c, 0, -40);    // <-- shift everything up by 40px when drawing.
[self.view.layer renderInContext:c];
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

This will render the retina display image context.

whyoz
  • 5,168
  • 47
  • 53
0

Just do something like this, way easier than all those complex calculations

+ (UIImage *)imageWithView:(UIView *)view {
    UIGraphicsBeginImageContextWithOptions([view bounds].size, NO, [[UIScreen mainScreen] scale]);

    [[view layer] renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return result;
}
Saren Inden
  • 3,450
  • 4
  • 32
  • 45
0

Here is Swift version

//Capture Screen
func capture()->UIImage {

    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, UIScreen.mainScreen().scale)
    self.view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image

}
bpolat
  • 3,879
  • 20
  • 26
0

swift version

    UIGraphicsBeginImageContext(self.view.bounds.size)
    let image: CGContextRef = UIGraphicsGetCurrentContext()!
    CGContextTranslateCTM(image, 0, -40)
    // <-- shift everything up by 40px when drawing.
    self.view.layer.renderInContext(image)
    let viewImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil)
Ahmed Safadi
  • 433
  • 1
  • 6
  • 19