ios - UIView drawViewHierarchyInRect 给出像素化结果?

标签 ios objective-c uiview

我正在使用 -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/

相关文章:

ios - 自动调整 UILabel 的高度

ios - View 卡在 MapView 后面

iphone - UIView 绕过事件到另一个 uiview

ios - swift : how to blurry a cell on tap in tableView

ios - 尝试使用 Swift 以编程方式显示 UIScrollView 而根本不显示

ios - Facebook SDK 使用 Graph API 1.0 版

IOS:一次管理一些 View Controller 并退出

ios - Objective-C 在另一个 XiB 中使用自定义 XiB

ios - 导航栏成为第一个表格 View 单元格

objective-c - NSTableCellView 没有获得第一个焦点