how to take a screenshot of the iPhone programmatically?

前端 未结 11 1841
谎友^
谎友^ 2020-11-28 08:01

Is it possible in objective C that we can take the screen shot of screen and stored this image in UIImage.

11条回答
  •  粉色の甜心
    2020-11-28 08:12

    The previous code assumes that the view to be captured lives on the main screen...it might not.

    Would this work to always capture the content of the main window? (warning: compiled in StackOverflow)

    
    - (UIImage *) captureScreen {
        UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
        CGRect rect = [keyWindow bounds];
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [keyWindow.layer renderInContext:context];   
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return img;
    }
    

提交回复
热议问题