Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

programmatic screenshot misrepresenting nearest-neighbor

I'm encountering an inconsistency between a screenshot (programmatically using the code below) and what's actually on screen when an image is zoomed in (very far in my case) and rendered nearest-neighbor to preserve the hard edges. I got the following screenshot code from these forums, but what gets saved is a (bilinear?) rendering of the image instead of nearest-neighbor.

UIGraphicsBeginImageContextWithOptions([[UIScreen mainScreen] bounds].size, NO, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *imageView = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(imageView, nil, nil, nil);

the way I do nearest-neighbor is with the following line in ViewDidLoad

automataView.layer.magnificationFilter = kCAFilterNearest;

(automataView is a UIImageView with a GIF file in it, and it's added as a subview to a UIScrollView which handles the zooming)

Here are two images, the first is what is on the screen, the second is what gets saved using the above screenshot code. (sorry for the links - "as a spam prevention mechanism, new users aren't allowed to post images")

http://www.flickr.com/photos/51983059@N08/8358662379/

http://www.flickr.com/photos/51983059@N08/8358662715/

Thank you for any help!

like image 776
Robby Avatar asked Jan 07 '13 23:01

Robby


1 Answers

In case the question is still relevant.

I would have thought that interpolation is something you set in the object that draws itself in the context, but it seems the context itself does some interpolation, it has the attribute CGInterpolationQuality with getter and setter. So the following change to your snippet works for me:

UIGraphicsBeginImageContextWithOptions([[UIApplication sharedApplication] keyWindow].bounds.size, NO, 0.0);
CGContextRef cgr = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(cgr, kCGInterpolationNone);
[self.view.layer renderInContext:cgr];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
like image 154
HBu Avatar answered Nov 02 '22 22:11

HBu