gpt4 book ai didi

iphone - iPad 上捕获 View 上下文的质量低下

转载 作者:可可西里 更新时间:2023-11-01 04:06:40 25 4
gpt4 key购买 nike

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

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

最佳答案

我想您需要使用 UIGraphicsBeginImageContextWithOptions 以视网膜分辨率(960x640 或 2048x1536)捕获 UIView/UIWindow 并将其缩放参数设置为 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();

这会以 95% 的质量将 UIImage 以 jpg 格式保存在应用程序的文档文件夹中

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

关于iphone - iPad 上捕获 View 上下文的质量低下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8891772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com