【问题标题】:Issue With drawViewHierarchyInRect: afterScreenUpdates while taking snapshotdrawViewHierarchyInRect 的问题:在拍摄快照时出现 afterScreenUpdates
【发布时间】:2017-08-12 14:00:32
【问题描述】:

我在拍摄UIViewCAEmitterLayer 的快照时遇到问题。下面是我用来拍摄快照的代码:

这里editingView是我的UIView

UIGraphicsBeginImageContextWithOptions(editingView.bounds.size, NO, 0.0);
[editingView drawViewHierarchyInRect:editingView.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

请通过以下链接获取 Gif 图片

链接For afterScreenUpdates:NO (Gif) 在这种情况下我缺少快照数据

For afterScreenUpdates:YES (Gif) 的链接在这种情况下 uiview 闪烁然后更新并且还面临性能问题。

【问题讨论】:

    标签: objective-c uiview uiimage uigraphicscontext


    【解决方案1】:

    我过去也遇到过类似的问题。以下可能对您有用:

    UIGraphicsBeginImageContextWithOptions(editingView.bounds.size, NO, 0.0);
    [editingView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    请注意,您可能会在屏幕截图中失去一些准确性,因为它可能会排除模糊和一些核心动画功能。

    编辑:

    renderInContextdrawViewHierarchyInRect 似乎都存在问题/权衡。下面的代码可能值得一试(取自here供参考):

    - (CGContextRef) createBitmapContextOfSize:(CGSize) size {
       CGContextRef    context = NULL;
       CGColorSpaceRef colorSpace;
       int             bitmapByteCount;
       int             bitmapBytesPerRow;
    
       bitmapBytesPerRow   = (size.width * 4);
       bitmapByteCount     = (bitmapBytesPerRow * size.height);
       colorSpace = CGColorSpaceCreateDeviceRGB();
       if (bitmapData != NULL) {
           free(bitmapData);
       }
       bitmapData = malloc( bitmapByteCount );
       if (bitmapData == NULL) {
           fprintf (stderr, "Memory not allocated!");
           return NULL;
       }
    
       context = CGBitmapContextCreate (bitmapData,
                                        size.width,
                                        size.height,
                                        8,      // bits per component
                                        bitmapBytesPerRow,
                                        colorSpace,
                                        kCGImageAlphaNoneSkipFirst);
    
       CGContextSetAllowsAntialiasing(context,NO);
       if (context== NULL) {
           free (bitmapData);
           fprintf (stderr, "Context not created!");
           return NULL;
       }
       CGColorSpaceRelease( colorSpace );
    
       CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, size.height);
       CGContextConcatCTM(context, flipVertical);
    
       return context;
    }
    

    那么你可以这样做:

    CGContextRef context = [self createBitmapContextOfSize:editingView.bounds.size];
    [editingView.layer renderInContext:context];
    
    CGImageRef cgImage = CGBitmapContextCreateImage(context);
    UIImage* background = [UIImage imageWithCGImage: cgImage];
    CGImageRelease(cgImage);
    

    【讨论】:

    • 嗨艾伦普尔,我试过这个。但我未能与 UIView 一起获得 CAEmitterLayer 的快照。为此,我找到了一些与核心动画相关的东西,你可以参考这个链接以获得快速的想法stackoverflow.com/questions/11926690/…。请帮我解决这个问题。请浏览我提供的 gif 链接
    • 这两种解决方案似乎都存在问题/权衡取舍。我已经用可能的替代方法更新了我的答案。该代码来自一篇相当旧的文章,因此它可能有效,也可能无效。
    猜你喜欢
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多