UIView drawViewHierarchyInRect给出的结果为何会出现像素化的效果?

3
我正在使用-drawViewHierarchyInRect:afterScreenUpdates:view绘制到UIImage中,但无论我做什么,结果总是非常低的分辨率,因为图像似乎需要与原始视图大小相同? UIView的大小为屏幕大小(在此情况下为375 x 667),内容比例因子为2.0f。
但是,当我使用-drawViewHierarchyInRect:afterScreenUpdates:view绘制到image上时,它会被调整大小(非常糟糕),即使内容分辨率更高。
我正在使用比例为0.0的UIGraphicsBeginImageContextWithOptions创建image,所以我认为它应该绘制得很好,但没有效果。如何绘制视图层次结构而不失去细节?
编辑: 这是一些示例代码(来自下面的答案,给出了相同的结果)。尝试绘制包含高分辨率图像的UIView层次结构时,它给我带来了可怕的缩小结果:
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0);
[self.view drawViewHierarchyInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) afterScreenUpdates:NO];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
imgView.frame = self.view.bounds;
[self.view addSubview:imgView];
[self.view bringSubviewToFront:imgView];

你能把你的代码复制粘贴到你的问题中吗? - Tobias
1个回答

0

这段代码完美运行。它会截取当前view的屏幕截图,并将其放置在新创建的UIImageView中。请检查一下。

UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0);
[self.view drawViewHierarchyInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) afterScreenUpdates:NO];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
imgView.frame = self.view.bounds;
[self.view addSubview:imgView];
[self.view bringSubviewToFront:imgView];

这实际上就是我正在做的事情。在包含高分辨率图像的UIView上运行此代码会产生可怕的缩小结果,因为它试图将其适应屏幕分辨率(不带比例),而后者要低得多。 - Rob
请问您能否分享更多细节?我无法在我的设备上复制像素化的图像。也许您只在特定设备或模拟器上遇到了这个问题? - Azat
我不太确定如何解释这个问题。我尝试在多个设备上使用iOS7和iOS8进行测试。例如,如果我有一个包含高分辨率图像的UIImageView的UIView,然后使用drawViewHierarchyInRect方法,图像会被严重降低缩放。看起来好像是为了与UIView的大小匹配(但没有缩放?!),即使在显示时它的质量是完美的。我确定我做错了什么,但是无法确定具体问题所在。 - Rob
现在我已经将巨大的“图像”放到我的“视图”中,并再次运行了该代码,一切都很好。看起来你的问题出在另一个地方。 - Azat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接