openfrog
openfrog

Reputation: 40735

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?

Upvotes: 4

Views: 1252

Answers (2)

openfrog
openfrog

Reputation: 40735

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);

Upvotes: 1

graver
graver

Reputation: 15213

The correct one would be:

UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0f);

Upvotes: 14

Related Questions