Search code examples
objective-ciosscreenshot

How to take a round screen shot?


I've seen a lot of questions and answers about screenshot, I know how to do it, this is not my problem, this is my code:

- (void)takeScreenShot
{
    //UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 340), YES, 0.);
    UIGraphicsBeginImageContext(CGSizeMake(320, 480));
    [self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

How is it possible to take a round screen shot instead of just 320x480 square frame?


Solution

  • You could try this:

    - (UIImage *)takeRoundedScreenShot:(CGPoint)center
    {
      float cornerRadius = self.view.window.layer.cornerRadius;
      CGAffineTransform savedTransform = self.view.window.transform;
    
      UIGraphicsBeginImageContext(CGSizeMake(320, 320));
      self.view.window.layer.cornerRadius = 160;
      self.view.window.clipsToBounds = YES;
      self.view.window.transform = CGAffineTransformMakeTranslation(-(center.x-160), -(center.y-160));
      [self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
      UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
    
      self.view.window.transform = savedTransform;
      self.view.window.layer.cornerRadius = cornerRadius.
    
      return viewImage;
    }