Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force -drawViewHierarchyInRect:afterScreenUpdates: to take snapshots at @2x resolution?

-drawViewHierarchyInRect:afterScreenUpdates: is a fast way in iOS 7 to take a snapshot of a view hierarchy.

It takes snapshots but with @1x resolution. The snapshots look pixellated and blurry on an iPhone 5S. The view from which I create a snapshot is not transformed.

I don't want to blur it and want good quality as seen on screen. Here is how I capture it:

UIGraphicsBeginImageContext(self.bounds.size);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

I also tried:

UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, self.contentScaleFactor);

which still renders with low @1x quality.

Is there another way to configure the image context so it's @2x resolution?

like image 305
openfrog Avatar asked Dec 07 '22 03:12

openfrog


2 Answers

The correct one would be:

UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0f);
like image 168
graver Avatar answered Dec 08 '22 15:12

graver


OK, so the problem was that self.contentScaleFactor returns a wrong value on a retina display device. It is 1 where it should be 2.

This works, but of course it's less than ideal because it has no fallback for non-retina devices.

UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 2);
like image 23
openfrog Avatar answered Dec 08 '22 17:12

openfrog