iPad 上的捕获视图上下文质量低下 [英] Low quality of capture view context on iPad
问题描述
我需要捕获特定的 UIView ,但结果是低质量的如何解决这个问题并提高质量?
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();
推荐答案
我猜你需要以视网膜分辨率(960x640 或 2048x1536)捕获 UIView/UIWindow 使用 UIGraphicsBeginImageContextWithOptions 并将其缩放参数设置为 0.0f 使其以原生分辨率捕获(适用于 iPhone 4 及更新机型或 iPad 3 的视网膜).
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).
此代码以原始分辨率捕获您的 UIView
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();
这会将 UIImage 以 jpg 格式以 95% 的质量保存在应用程序的文件夹中
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];
这篇关于iPad 上的捕获视图上下文质量低下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!