UIView drawViewHierarchyInRect给出像素化结果?

时间:2015-05-10 17:45:21

标签: ios objective-c uiview

我正在使用-drawViewHierarchyInRect:afterScreenUpdates:view绘制到UIImage中,但似乎无论我做什么,结果始终是一个非常低的分辨率,因为图像似乎需要与原始视图的大小相同吗?

UIView是屏幕的大小(在本例中为375 x 667),内容比例因子为2.0f。

但是,当我使用-drawViewHierarchyInRect:afterScreenUpdates:view绘制到image时,它会被调整(可怕地)到此分辨率,即使内容的分辨率要高得多。

我正在使用UIGraphicsBeginImageContextWithOptions以0.0的比例来创建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];

1 个答案:

答案 0 :(得分: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];