How to take a screenshot programmatically on iOS

前端 未结 20 2077
一生所求
一生所求 2020-11-22 01:42

I want a screenshot of the image on the screen saved into the saved photo library.

20条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 02:09

    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
    [self.myView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    NSData *imageData = UIImageJPEGRepresentation(image, 1.0 ); //you can use PNG too
    [imageData writeToFile:@"image1.jpeg" atomically:YES];
    

提交回复
热议问题