【问题标题】:Low quality of capture view context on iPadiPad 上的捕获视图上下文质量低下
【发布时间】:2012-02-12 02:07:25
【问题描述】:

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

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

【问题讨论】:

  • 你也可以试试这个 UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
  • 当你说“低质量”时,你到底是什么意思?图像看起来与屏幕上的不一样?您是否使用视网膜显示器,但捕获的图像不那么清晰?图像是否有颗粒感或变形?它到底有什么问题?
  • 您能否提供正在捕获的内容以及生成的图像的屏幕截图?
  • 您在致电UIGraphicsGetCurrentContext 之前是否将建议的UIGraphicsBeginImageContextWithOptions 放在了前面?
  • 您确定问题出在您的捕获代码中吗? UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0); 直接截图。我的意思是:您如何测试结果-通过从代码中显示它们还是检查文件?你是在模拟器还是实际设备上这样做...

标签: iphone ios xcode ipad sdk


【解决方案1】:

我猜您需要以视网膜分辨率(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();

这个对于全屏(关键窗口)也是如此

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% 的质量保存在应用程序的文件夹中

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 1970-01-01
    相关资源
    最近更新 更多