Ios 如果使用UIView,如何提高屏幕截图的质量

Ios 如果使用UIView,如何提高屏幕截图的质量,ios,objective-c,uiview,Ios,Objective C,Uiview,在我的应用程序中,我需要能够共享UIView的屏幕截图。这是我用来截图的代码: CGRect viewFrame = [view bounds]; UIGraphicsBeginImageContextWithOptions(viewFrame.size, YES, 0.0); CGContextRef context = UIGraphicsGetCurrentContext(); [view.layer renderInContext:context]; UIImage *viewSc

在我的应用程序中,我需要能够共享UIView的屏幕截图。这是我用来截图的代码:

CGRect viewFrame = [view bounds];
UIGraphicsBeginImageContextWithOptions(viewFrame.size, YES, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];   
UIImage *viewScreenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
我对生成的图像质量不满意。这里有一个例子。我不喜欢文本和图像周围的噪音/模糊


有人能告诉我如何提高质量吗?或者就是这样吗?

您提供的代码片段是正确的。它将产生完美的图像没有人工制品。但是,当您将其保存到文件时,必须使用无损格式。图像中的模糊是由有损压缩引起的

我同意其他答案。只是一个简短的说明。在iOS7+中,您可以使用以下代码代替您的代码。速度要快得多:

UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0f);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
UIImage * snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return snapshotImage;

这与您提供的代码无关。在此阶段,
ui图像
不会出现质量损失。当您尝试将其保存为JPG而不是PNG时(因此当您执行
UIImage
->
NSData
时),就会发生丢失。我同意。很可能您看到的是JPEG伪影。PNG是屏幕截图的更好选择。