Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Screenshot part of the screen

I have an App that takes a screenshot of a UIImageView with the following code:

-(IBAction) screenShot: (id) sender{

 UIGraphicsBeginImageContext(sshot.frame.size);
 [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
 UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 UIImageWriteToSavedPhotosAlbum(viewImage,nil, nil, nil);


}

This works well but I need to be able to position where I take the screenshot basically I need to grad only a third of the screen (center portion). I tried using

UIGraphicsBeginImageContext(CGSize 150,150);

But have found that every thing is taken from 0,0 coordinates, has anyone any idea how to position this correctly.

like image 399
yeha Avatar asked Oct 02 '12 09:10

yeha


People also ask

How do I screenshot a specific part of my iPhone screen?

Press the side button and the volume up button at the same time. Quickly release both buttons. After you take a screenshot, a thumbnail temporarily appears in the lower-left corner of your screen.

How do I screenshot just a portion of my screen?

Press Ctrl + PrtScn keys. The entire screen changes to gray including the open menu. Select Mode, or in earlier versions of Windows, select the arrow next to the New button. Select the kind of snip you want, and then select the area of the screen capture that you want to capture.

Does iOS have a snipping tool?

The CloudApp Snipping Tool is available for Mac, iOS and Chrome.


2 Answers

Well the screenshot is taken from a canvas you draw. So instead of drawing your layer in the whole context, with a reference to top left corner, you will draw it where you want to take the screenshot....

//first we will make an UIImage from your view
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//now we will position the image, X/Y away from top left corner to get the portion we want
UIGraphicsBeginImageContext(sshot.frame.size);
[sourceImage drawAtPoint:CGPointMake(-50, -100)];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(croppedImage,nil, nil, nil);
like image 92
Lefteris Avatar answered Nov 05 '22 14:11

Lefteris


From this

UIGraphicsBeginImageContext(sshot.frame.size);
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(c, 150, 150);    // <-- shift everything up to required position when drawing.
[self.view.layer renderInContext:c];
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
like image 38
AlienMonkeyCoder Avatar answered Nov 05 '22 15:11

AlienMonkeyCoder