2017-03-21 57 views
0

我面临UIView以及CAEmitterLayer的快照问题。下面是我使用采取快照代码:使用drawViewHierarchyInRect执行问题:afterScreenUpdates拍摄快照时

这里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闪烁,然后更新,也面临性能问题。

回答

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); 
+0

嗨阿兰·普尔,我尝试这样做。但是我无法获得CAEmitterLayer和UIView的快照。为此我找到了一些相关的核心动画,你可以参考这个链接快速的想法http://stackoverflow.com/questions/11926690/caemitterlayer-not-rendering-when-renderincontext-of-superlayer-is-called。并请帮助我这个。并请通过我提供的gif链接通过 –

+0

这两个解决方案似乎存在问题/权衡。我已经用可能的选择更新了我的答案。代码来自一篇相当古老的文章,所以它可能会或可能不会工作。 –

+0

不适用于me.missing CAEmitterLayer –