How Do I Take a Screen Shot of a UIView?

后端 未结 15 2603
-上瘾入骨i
-上瘾入骨i 2020-11-22 09:28

I am wondering how my iPhone app can take a screen shot of a specific UIView as a UIImage.

I tried this code but all I get is a blank image

15条回答
  •  失恋的感觉
    2020-11-22 09:49

    - (void)drawRect:(CGRect)rect {
      UIGraphicsBeginImageContext(self.bounds.size);    
      [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
      UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);  
    }
    

    This method may put in your Controller class.

提交回复
热议问题