How to take a screenshot programmatically on iOS

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

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

20条回答
  •  萌比男神i
    2020-11-22 02:22

    I think the following snippet would help if you want to take a full screen(except for status bar),just replace AppDelegate with your app delegate name if necessary.

    - (UIImage *)captureFullScreen {
    
        AppDelegate *_appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
            // for retina-display
            UIGraphicsBeginImageContextWithOptions(_appDelegate.window.bounds.size, NO, [UIScreen mainScreen].scale);
            [_appDelegate.window drawViewHierarchyInRect:_appDelegate.window.bounds afterScreenUpdates:NO];
        } else {
            // non-retina-display
            UIGraphicsBeginImageContext(_bodyView.bounds.size);
            [_appDelegate.window drawViewHierarchyInRect:_appDelegate.window.bounds afterScreenUpdates:NO];
        }
    
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    

提交回复
热议问题