首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIView drawViewHierarchyInRect给出像素化的结果?

UIView drawViewHierarchyInRect给出像素化的结果?
EN

Stack Overflow用户
提问于 2015-05-11 01:45:21
回答 1查看 715关注 0票数 3

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

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

但是,当我使用-drawViewHierarchyInRect:afterScreenUpdates:view绘制到image上时,它的大小(可怕地)调整到了这个分辨率,尽管内容的分辨率要高得多。

我使用比例为0.0的UIGraphicsBeginImageContextWithOptions来创建image,所以我认为它应该绘制得很好,但没有成功。如何在不丢失细节的情况下绘制视图层次?

编辑:以下是一些示例代码(来自下面的答案,结果相同)。当我试图绘制包含高分辨率图像的UIView层次结构时,它会给出非常小的结果:

代码语言:javascript
复制
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];
EN

回答 1

Stack Overflow用户

发布于 2015-05-11 02:38:32

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

代码语言:javascript
复制
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];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30154597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档