iPad上的低质量捕获视图上下文

Mc.*_*ver 5 iphone sdk xcode ipad ios

我需要捕获特定的UIView,但结果是低质量如何解决这个问题并提高质量?

UIGraphicsBeginImageContext(captureView.bounds.size);
    [captureView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil);
    UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

Tib*_*abo 22

我想你需要在视网膜分辨率(960x640或2048x1536)中捕获UIView/UIWindow使用UIGraphicsBeginImageContextWithOptions并设置其缩放参数0.0f使其以原始分辨率捕获(适用于iPhone 4及更高版本或iPad 3的视网膜).

此代码以原始分辨率捕获您的UIView

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

这个对全屏(关键窗口)也一样

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();
Run Code Online (Sandbox Code Playgroud)

这样就可以在应用程序的文档文件夹中以99%的质量保存Upgmage的jpg格式

NSString  *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/capturedImage.jpg"]];    
[UIImageJPEGRepresentation(capturedImage, 0.95) writeToFile:imagePath atomically:YES];
Run Code Online (Sandbox Code Playgroud)