Reputation: 31
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.
Can anyone tell me how to improve the quality, por favor? Or is this just how it is?
Upvotes: 0
Views: 223
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
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