How to take a screenshot programmatically on iOS

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

    Below method works for OPENGL objects also

    //iOS7 or above
    - (UIImage *) screenshot {
    
        CGSize size = CGSizeMake(your_width, your_height);
    
        UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    
        CGRect rec = CGRectMake(0, 0, your_width, your_height);
        [_viewController.view drawViewHierarchyInRect:rec afterScreenUpdates:YES];
    
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    

提交回复
热议问题