Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何保存UIView内容(屏幕截图)而不减少它';s质量_Ios_Objective C_Uiview - Fatal编程技术网

Ios 如何保存UIView内容(屏幕截图)而不减少它';s质量

Ios 如何保存UIView内容(屏幕截图)而不减少它';s质量,ios,objective-c,uiview,Ios,Objective C,Uiview,我有一个ui视图,我想将其内容保存到图像中,我成功地使用UIGraphicsBeginImageContext和UIGraphicsGetImageFromCurrentImageContext()实现了这一点,但问题是图像质量降低了。有没有一种方法可以在不降低图像质量的情况下将屏幕截图/保存UIView内容保存到图像中 以下是我的代码片段: UIGraphicsBeginImageContext(self.myView.frame.size); [self.myview.layer rende

我有一个
ui视图
,我想将其内容保存到图像中,我成功地使用
UIGraphicsBeginImageContext
UIGraphicsGetImageFromCurrentImageContext()
实现了这一点,但问题是图像质量降低了。有没有一种方法可以在不降低图像质量的情况下将屏幕截图/保存
UIView
内容保存到图像中

以下是我的代码片段:

UIGraphicsBeginImageContext(self.myView.frame.size);
[self.myview.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
请尝试以下代码:

UIGraphicsBeginImageContextWithOptions(YourView.bounds.size, NO, 0);

[YourView drawViewHierarchyInRect:YourView.bounds afterScreenUpdates:YES];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
这段代码真是太棒了

是一个很好的解决方案,但问题的根源在于您的视图位于视网膜屏幕上(因此比例因子为2),并且您正在创建比例因子为1的图形上下文。各国:

此函数相当于调用
UIGraphicsBeginImageContextWithOptions
函数,不透明参数设置为NO,比例因子为
1.0


相反,使用
UIGraphicsBeginImageContextWithOptions
,比例为
0
,这相当于通过
[[UIScreen mainScreen]scale]

的比例,质量会以什么方式降低?我有一个
UImageView
包含图像作为
UIView
的子视图,当我保存它时,图像的质量会降低。有很多质量度量,但我推断问题在于下采样:参见我的答案。