YogevSitton
YogevSitton

Reputation: 10108

Losing quality when capturing screen

I'm capturing a part of the screen that the app is currently showing using the following code:

UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIGraphicsBeginImageContext(CGSizeMake(320, 320));
[sourceImage drawAtPoint:CGPointMake(0, -45)];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

The problem is, the result has reduced quality when comparing to the original screen. How can I prevent the quality loss?

Upvotes: 1

Views: 101

Answers (1)

lu yuan
lu yuan

Reputation: 7227

For you info.

// Create a graphics context with the target size
// On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
// On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
CGSize imageSize = [[UIScreen mainScreen] bounds].size;
if (NULL != UIGraphicsBeginImageContextWithOptions)
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
else
    UIGraphicsBeginImageContext(imageSize);

CGContextRef context = UIGraphicsGetCurrentContext();

Upvotes: 4

Related Questions