Search code examples
iphoneimage-processingios4uiimage

Getting a resized screenshot from a UIView


I'm trying to take a screenshot of a UIView shrunk down to thumbnail size with the following code,

    UIGraphicsBeginImageContext(size);
    [canvas.layer renderInContext:UIGraphicsGetCurrentContext()];
    result = [UIGraphicsGetImageFromCurrentImageContext() retain];
    UIGraphicsEndImageContext();

The above code will simply grab the top left portion of the view in the original unshrunk size instead.

I'm sure I've done this before, but I just can't get it working. Anyone know what's off here?


Solution

  • Supposing that you have a CGSize origSize which is the original size (e.g. 768x1024) and a CGSize size which is the required size, this can be done like so:

    CGFloat scaleX = size.width / origSize.width;
    CGFloat scaleY = size.height / origSize.height;
    UIGraphicsBeginImageContextWithOptions(origSize, NO, scaleX > scaleY ? scaleY : scaleX);
    [canvas.layer renderInContext:UIGraphicsGetCurrentContext()];
    result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    Note that we're using origSize in the begun context, not size. The scale affects the size as well.

    Update (roughly a year later): note that this technique interferes with (or is interefered by) transforms on the UIView being snapshotted. If the above is not working and you're doing scale transforms on the view (or its layer), you may wanna go with this solution: How to scale down a UIImage and make it crispy / sharp at the same time instead of blurry?