How to take a screenshot using iOS code?

How to take a screenshot programmatically?

+3
source share
2 answers

You can use UIGraphicsBeginImageContextfor this purpose.

For instance:

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage*theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData*theImageData = UIImageJPEGRepresentation(theImage, 1.0 ); //you can use PNG too
[theImageData writeToFile:@"example.jpeg" atomically:YES];

Here

1. First, I take the context of the image, I gave it how myView, which is a web view, you can provide whatever you want. It takes a web view image that can be seen on the screen.

2 Using UIGraphicsGetImageFromCurrentImageContext(), I will convert a screenshot of my web view into an image.

3. Using UIGraphicsEndImageContext(), I ask you to complete the context.

4. NSData, . NSData , .

EDIT: , :

UIImageWriteToSavedPhotosAlbum(theImage,nil,NULL,NULL); UIGraphicsEndImageContext();

+14

. .

, ,

  • (, , )
  • , .
  • , !
+1

All Articles