Search code examples
iphoneiosuiviewscreenshot

iPhone iOS how to render a screenshot with a different resolution or scale?


I'm trying to save a screenshot of my iPhone screen, but the result is coming out at 1x scale.

For example a 320x480 pixels screenshot is 320x480 pixels, when displayed on a retina display, it looks fuzzy. But if I take a screenshot with the home screen and the power button, the resulting image is 640x960 and looks perfect on a retina display. How can I take screenshots of the screen taking into account the scale factor of the screen?

Thank you!


Solution

  • Try This:
    
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
            UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale);
        else
            UIGraphicsBeginImageContext(self.window.bounds.size);
    
        [self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
            UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            NSData * data = UIImagePNGRepresentation(image);
            [data writeToFile:@"foo.png" atomically:YES];