iOS Quick Tip: Take a ScreenShot at Different Resolutions

Stephen Zaharuk / Wednesday, April 30, 2014

I've talked about all the tricks you can do with images before, even about how to take screen shots of a particular UIView. However, today i'm going to dive into that just a little bit more and show you how you can take that screen shot at different resolutions. 

Basically, not much will be different from our basic screen shot method:

+(UIImage*)screenShotOf:(UIView*)view
{
   UIGraphicsBeginImageContext(view.frame.size);
   [view.layer renderInContext:UIGraphicsGetCurrentContext()];

   UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

   UIGraphicsEndImageContext();

   return img;
}

Instead, we're just going to add one additional parameter to our method. And we're going to use a different overload of UIGraphicsBeginImageContext.

+(UIImage*)screenShotOf:(UIView*)view atScale:(CGFloat)scale
{
   UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, scale);
  [view.layer renderInContext:UIGraphicsGetCurrentContext()];

   UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
 
  UIGraphicsEndImageContext();

  return img;
}

So, what are valid values for the scale that we can pass in?

The value 2 would be a Retina Resolution:

The value 1 would be a non retina resolution:

And a value of .5 would be half of that:

This is particularly useful, if the screen shot your grabbing of a view is only going to be used as a thumbnail. Thus decreasing the memory usage of your app. 

Hope you found this useful.

By Stephen Zaharuk (SteveZ)