Cannex
Cannex

Reputation: 31

How to improve the quality of a screenshot if a UIView

In my application, I need to be able to share a screenshot of a UIView. This is the code I am using to take the screenshot:

CGRect viewFrame = [view bounds];
UIGraphicsBeginImageContextWithOptions(viewFrame.size, YES, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];   
UIImage *viewScreenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

I'm not satisfied with the quality of the image produced. Here is an example. I'm not fond of the noise/blurriness around the text and the image.

UIView screenshot example

Can anyone tell me how to improve the quality, por favor? Or is this just how it is?

Upvotes: 0

Views: 223

Answers (2)

jomafer
jomafer

Reputation: 2725

I agree with the other answers. Just a quick note. In iOS7+ you can use the following code instead of yours. It is much faster:

UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0f);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
UIImage * snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return snapshotImage;

Upvotes: 1

Juraj Antas
Juraj Antas

Reputation: 2712

The code fragment you provided is correct. It will produce perfect image without artefacts. However you have to use lossless format when you are saving it to file. The bluriness you see in your image is caused with lossy compression.

Upvotes: 1

Related Questions