0

I have a UIImageView in my view controller whose screeshot I need to take. There is a UILabel on it as well. UIImageView doesn't cover the whole page just part of it. How can I take screenshot of just section of the screen?

I bet this question has been asked before but I looked into this post How to use UIGraphicsBeginImageContextWithOptions? and it was nothing but confusing with undefined variables

enter image description here

so far the code I have takes the screenshot of the whole thing. (not what I want). I tried playing with screenRect with no effect. I thought about cropping picture after taking the whole screen screenshot but the problem is that the crop sizes will be different for each device iphone3, iphone4, iphone5, iPad 1,2,3 etc.

So my question is there a way to take a screenshot of a section of screen without going through cropping route? If cropping is the only way to go then is there a way I can reliably cut the picture without worrying about different iOS devices with high/low pixels?

testBgImgVw = [[UIImageView alloc] initWithFrame:CGRectMake(0, 80, 320, 220)]; .... -(IBAction) takeScreenshot { CGRect screenRect = [[UIScreen mainScreen] bounds]; if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { //its iphone screenRect = CGRectMake(0, 0, 320, 480); } else { //its pad screenRect = CGRectMake(0, 0, 768, 1024); } // This code is for high resolution screenshot UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, 0.0); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); NSData* imageData = UIImageJPEGRepresentation(viewImage, 1.0); NSString* incrementedImgStr = [NSString stringWithFormat: @"UserScreenShotPic.jpg"]; NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString* documentsDirectory = [paths objectAtIndex:0]; // Now we get the full path to the file NSString* fullPathToFile2 = [documentsDirectory stringByAppendingPathComponent:incrementedImgStr]; [imageData writeToFile:fullPathToFile2 atomically:NO]; } 
3
  • do you wanna capture the image & the label? Commented Apr 19, 2013 at 16:41
  • which section do you wanna crop? Commented Apr 19, 2013 at 16:42
  • I want to capture the image and label. The image and label are usually all in one area i.e. between (0, 80, 320, 220) coordinates Commented Apr 19, 2013 at 16:51

1 Answer 1

1

ok if you just only want to capture the image & the label on the image ( if you can make sure the label is always on the image), then

add the label on the imageview (imageview addSubview:label)

then render it into a new image

UIGraphicsBeginImageContextWithOptions(screenRect.size, NO,[[UIScreen mainScreen]scale]); [imageView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 

done :)

if you wanna take a shot of several uiviews(imageview,label, etc), the best way is provide a UIView canvas

UIView *canvasView = [[UIView alloc]......] [self.view addSubview:canvasView]; 

and add every view you wanna render on it , finally render it into a new image you want

Sign up to request clarification or add additional context in comments.

7 Comments

that worked 50% - it took the screenshot of the image view section which is great but for some reason adding the label as subview didn't capture the label
it will capture the label if the label is the subview of the imageview, unless you calculate the wrong position that the label is no shown in the visibal rect of the imageview
you should recalculate the relative position from the label to the imageview first, until the label is displayed right to the image you provide above
ok, this will take a lot of trail and error. To compound the problem I let users move label around. Now I have to figure out where did they leave the label relative to the image. Just adding this line doesn't do anything (in terms of screen capture) [testBgImgVw addSubview:customLabel1];
I guess my only hope is to crop the whole screen capture starting at certain X,Y coordinates in this case (0,80).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.