【问题标题】:UIView drawViewHierarchyInRect giving pixelated results?UIView drawViewHierarchyInRect 给出像素化结果?
【发布时间】:2015-07-21 04:27:33
【问题描述】:

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

【问题讨论】:

  • 您可以将代码粘贴到您的问题中吗?

标签: ios objective-c uiview


【解决方案1】:

此代码完美运行。它截取当前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 运行它会产生非常缩小的结果,因为它试图将其适应屏幕分辨率(没有比例),而屏幕分辨率要低得多。
  • 你能分享更多细节吗?我无法在我的机器中重现像素化图像。也许您仅在特定设备或仅模拟器等上体验过?
  • 我不太清楚如何解释。我已经在 iOS7 和 iOS8 的多台设备上进行了尝试。例如,如果我有一个带有高分辨率图像的 UIImageView 的 UIView,那么使用 drawViewHierarchyInRect,图像被缩小得非常糟糕。看起来这样做是为了匹配 UIView 的大小(没有缩放?!),即使它显示时它的质量是完美的。我确定我做错了什么,但我不能指手画脚。
  • 现在我已经将巨大的image 放入我的view 并再次运行该代码,一切正常。似乎您的问题在另一个地方的某个地方
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-17
  • 1970-01-01
  • 1970-01-01
  • 2020-03-29
相关资源
最近更新 更多