title: Taking screenshot of Part of Screen
body: <p>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?</p>
  
  <p>I bet this question has been asked before but I looked into this post <a href="https://stackoverflow.com/questions/5146008/how-to-use-uigraphicsbeginimagecontextwithoptions">How to use UIGraphicsBeginImageContextWithOptions?</a> and it was nothing but confusing with undefined variables</p>
  
  <p><img src="https://i.stack.imgur.com/nsR5K.png" alt="enter image description here"></p>
  
  <p>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. </p>
  
  <p>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?</p>
  
  <pre><code>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];
  
  
      }
  </code></pre>