How to take a screenshot programmatically on iOS

前端 未结 20 2098
一生所求
一生所求 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:25

    Get Screenshot From View

    -(UIImage *)getScreenshotImage {
        if ([[UIScreen mainScreen] scale] == 2.0) {
            UIGraphicsBeginImageContextWithOptions(self.view.frame.size, FALSE, 2.0);
        } else {
            UIGraphicsBeginImageContextWithOptions(self.view.frame.size, FALSE, 1.0);
        }
    
        [self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    
        UIImage * result = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return result;
    }
    

    Save Image to Photos

    UIImageWriteToSavedPhotosAlbum(YOUR_IMAGE, nil, nil, nil);
    

    How-To

    UIImageWriteToSavedPhotosAlbum([self getScreenshotImage], nil, nil, nil);
    

提交回复
热议问题