我正在使用 -drawViewHierarchyInRect:afterScreenUpdates:
将 view
绘制到 UIImage
中,但似乎无论我做什么结果总是很低的分辨率,因为图像似乎需要与原始 View 的大小相同?
UIView
是屏幕的大小(在本例中为 375 x 667),内容比例因子为 2.0f。
但是当我使用 -drawViewHierarchyInRect:afterScreenUpdates:
将 view
绘制到 image
上时,它会(可怕地)调整到这个分辨率,即使内容的分辨率要高得多。
我正在使用比例为 0.0 的 UIGraphicsBeginImageContextWithOptions
来创建 image
,所以我认为它应该可以很好地绘制,但没有成功。如何绘制 View 层次结构而不丢失细节?
编辑: 这是一些示例代码(来自下面的答案,给出了相同的结果)。在尝试绘制包含高分辨率图像的 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];
最佳答案
此代码完美运行。它获取当前 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];
关于ios - UIView drawViewHierarchyInRect 给出像素化结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30154597/