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
的子视图,当我保存它时,图像的质量会降低。有很多质量度量,但我推断问题在于下采样:参见我的答案。