Objective c drawViewHierarchyInRect问题:拍摄快照时更新后的屏幕

Objective c drawViewHierarchyInRect问题:拍摄快照时更新后的屏幕,objective-c,uiview,uiimage,uigraphicscontext,Objective C,Uiview,Uiimage,Uigraphicscontext,我在拍摄UIView以及CAEmitterLayer的快照时遇到问题。下面是我用于拍摄快照的代码: 这里的editingView是我的UIView: UIGraphicsBeginImageContextWithOptions(editingView.bounds.size, NO, 0.0); [editingView drawViewHierarchyInRect:editingView.bounds afterScreenUpdates:YES]; UIImage *image = UIG

我在拍摄
UIView
以及
CAEmitterLayer
的快照时遇到问题。下面是我用于拍摄快照的代码:

这里的
editingView
是我的
UIView

UIGraphicsBeginImageContextWithOptions(editingView.bounds.size, NO, 0.0);
[editingView drawViewHierarchyInRect:editingView.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
请浏览下面的Gif图像链接

链接(Gif)在本例中,我缺少快照数据


在本例中,uiview的链接(Gif)闪烁,然后更新,并且还面临性能问题。

我过去也遇到过类似问题。以下内容可以作为一种变通方法:

UIGraphicsBeginImageContextWithOptions(editingView.bounds.size, NO, 0.0);
[editingView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
请注意,您可能会在屏幕捕获中失去一些准确性,因为它可能会排除模糊和一些核心动画功能

编辑:


renderInContext
drawViewHierarchyInRect
似乎都存在问题/权衡。以下代码可能值得一试(摘自以供参考):

然后你可以做:

CGContextRef context = [self createBitmapContextOfSize:editingView.bounds.size];
[editingView.layer renderInContext:context];

CGImageRef cgImage = CGBitmapContextCreateImage(context);
UIImage* background = [UIImage imageWithCGImage: cgImage];
CGImageRelease(cgImage);

嗨,艾伦·普尔,我试过这个。但我没能得到CAEmitterLayer和UIView的快照。为此,我找到了一些相关的核心动画,你可以参考这个链接的快速想法。关于这一点,请帮助我。请使用我提供的gif链接。这两种解决方案似乎都存在问题/权衡。我已经用一个可能的替代方案更新了我的答案。该代码来自一篇相当古老的文章,因此它可能有效,也可能无效。
CGContextRef context = [self createBitmapContextOfSize:editingView.bounds.size];
[editingView.layer renderInContext:context];

CGImageRef cgImage = CGBitmapContextCreateImage(context);
UIImage* background = [UIImage imageWithCGImage: cgImage];
CGImageRelease(cgImage);