Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Low quality of capture view context on iPad

I need capture specific UIView , but the result is in low quality how can fix this and increase the quality ?

UIGraphicsBeginImageContext(captureView.bounds.size);
    [captureView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil);
    UIGraphicsEndImageContext();
like image 821
iOS.Lover Avatar asked Jan 17 '12 08:01

iOS.Lover


1 Answers

I guess you need a capture a UIView/UIWindow in retina resolution (960x640 or 2048x1536) Using UIGraphicsBeginImageContextWithOptions and set its scale parameter 0.0f makes it capture in native resolution (retina for iPhone 4 and later or iPad 3).

This code capture your UIView in native resolution

CGRect rect = [captureView bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[captureView.layer renderInContext:context];   
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

This one does the same for full screen (key window)

UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
CGRect rect = [keyWindow bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[keyWindow.layer renderInContext:context];   
UIImage *capturedScreen = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

This saves the UIImage in jpg format with 95% quality in the app's document folder

NSString  *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/capturedImage.jpg"]];    
[UIImageJPEGRepresentation(capturedImage, 0.95) writeToFile:imagePath atomically:YES];
like image 131
Tibidabo Avatar answered Oct 18 '22 17:10

Tibidabo