如何在后台渲染CALayer

arl*_*dia 8 multithreading core-graphics copy-constructor calayer ios

我需要保存我的应用程序的屏幕截图,所以我设置了这样的代码,它的工作原理如下:

- (void)renderScreen {
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];

    CGSize outputSize = keyWindow.bounds.size;
    UIGraphicsBeginImageContext(outputSize);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);
    CALayer *layer = [keyWindow layer];
    [layer renderInContext:context];
    CGContextRestoreGState(context);

    UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // now save the screen image, etc...
}
Run Code Online (Sandbox Code Playgroud)

但是,当屏幕图像变得复杂(许多视图)时,在iPad 3上,renderInContext最多可能需要0.8秒,并且用户界面在此期间会锁定,这会干扰其他一些功能.所以我将渲染移动到后台线程,如下所示:

- (void)renderScreen {
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    CALayer *layer = [keyWindow layer];
    [self performSelectorInBackground:@selector(renderLayer:) withObject:layer];
}

- (void)renderLayer:(CALayer *)layer {
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];

    CGSize outputSize = keyWindow.bounds.size;
    UIGraphicsBeginImageContext(outputSize);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);
    [layer renderInContext:context];
    CGContextRestoreGState(context);

    UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // now save the screen image, etc...
}
Run Code Online (Sandbox Code Playgroud)

这允许接口再次平滑运行,但偶尔会导致renderInContext行上的EXC_BAD_ACCESS崩溃.我首先尝试检查图层!= nil和[layer respondsToSelector:@selector(renderInContext :)],这样我就可以避免崩溃,但这两个条件总是返回true.

然后我读了这个SO评论,说明在后台操作运行之前一个层可能会发生变异,并建议将该层的副本发送到后台操作.这SO回答这个让我开始,我结束了这一类的复制方法添加到CALayer的:

#import "QuartzCore/CALayer.h"

@interface CALayer (CALayerCopyable)
- (id)copy;
@end

@implementation CALayer (CALayerCopyable)

- (id)copy {
    CALayer *newLayer = [CALayer layer];
    newLayer.actions = [self.actions copy];
    newLayer.anchorPoint = self.anchorPoint;
    newLayer.anchorPointZ = self.anchorPointZ;
    newLayer.backgroundColor = self.backgroundColor;
    //newLayer.backgroundFilters = [self.backgroundFilters copy]; // iOS 5+
    newLayer.borderColor = self.borderColor;
    newLayer.borderWidth = self.borderWidth;
    newLayer.bounds = self.bounds;
    //newLayer.compositingFilter = self.compositingFilter; // iOS 5+
    newLayer.contents = [self.contents copy];
    newLayer.contentsCenter = self.contentsCenter;
    newLayer.contentsGravity = [self.contentsGravity copy];
    newLayer.contentsRect = self.contentsRect;
    //newLayer.contentsScale = self.contentsScale; // iOS 4+
    newLayer.cornerRadius = self.cornerRadius;
    newLayer.delegate = self.delegate;
    newLayer.doubleSided = self.doubleSided;
    newLayer.edgeAntialiasingMask = self.edgeAntialiasingMask;
    //newLayer.filters = [self.filters copy]; // iOS 5+
    newLayer.frame = self.frame;
    newLayer.geometryFlipped = self.geometryFlipped;
    newLayer.hidden = self.hidden;
    newLayer.magnificationFilter = [self.magnificationFilter copy];
    newLayer.mask = [self.mask copy]; // property is another CALayer
    newLayer.masksToBounds = self.masksToBounds;
    newLayer.minificationFilter = [self.minificationFilter copy];
    newLayer.minificationFilterBias = self.minificationFilterBias;
    newLayer.name = [self.name copy];
    newLayer.needsDisplayOnBoundsChange = self.needsDisplayOnBoundsChange;
    newLayer.opacity = self.opacity;
    newLayer.opaque = self.opaque;
    newLayer.position = self.position;
    newLayer.rasterizationScale = self.rasterizationScale;
    newLayer.shadowColor = self.shadowColor;
    newLayer.shadowOffset = self.shadowOffset;
    newLayer.shadowOpacity = self.shadowOpacity;
    newLayer.shadowPath = self.shadowPath;
    newLayer.shadowRadius = self.shadowRadius;
    newLayer.shouldRasterize = self.shouldRasterize;
    newLayer.style = [self.style copy];
    //newLayer.sublayers = [self.sublayers copy]; // this line makes the screen go blank
    newLayer.sublayerTransform = self.sublayerTransform;
    //newLayer.superlayer = self.superlayer; // read-only
    newLayer.transform = self.transform;
    //newLayer.visibleRect = self.visibleRect; // read-only
    newLayer.zPosition = self.zPosition;
    return newLayer;
}

@end
Run Code Online (Sandbox Code Playgroud)

然后我更新了renderScreen以将图层的副本发送到renderLayer:

- (void)renderScreen {
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    CALayer *layer = [keyWindow layer];
    CALayer *layerCopy = [layer copy];
    [self performSelectorInBackground:@selector(renderLayer:) withObject:layerCopy];
}
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,所有屏幕图像都是纯白色.显然我的复制方法不正确.那么有人可以帮我解决以下任何可能的解决方案吗?

  1. 如何为CALayer编写真正有效的复制方法?
  2. 如何检查传递给后台进程的层是否是renderInContext的有效目标?
  3. 在不锁定界面的情况下渲染复杂图层的任何其他方法?

更新:我根据Rob Napier建议使用initWithLayer重写了我的CALayerCopyable类别.简单地复制图层仍然给我一个纯白色输出,所以我添加了一个方法来递归复制所有子图层.然而,我仍然得到纯白色输出:

#import "QuartzCore/CALayer.h"

@interface CALayer (CALayerCopyable)
- (id)copy;
- (NSArray *)copySublayers:(NSArray *)sublayers;
@end

@implementation CALayer (CALayerCopyable)

- (id)copy {
    CALayer *newLayer = [[CALayer alloc] initWithLayer:self];
    newLayer.sublayers = [self copySublayers:self.sublayers];
    return newLayer;
}

- (NSArray *)copySublayers:(NSArray *)sublayers {
    NSMutableArray *newSublayers = [NSMutableArray arrayWithCapacity:[sublayers count]];
    for (CALayer *sublayer in sublayers) {
        [newSublayers addObject:[sublayer copy]];
    }
    return [NSArray arrayWithArray:newSublayers];
}

@end
Run Code Online (Sandbox Code Playgroud)

Rob*_*ier 2

为此,我将使用initWithLayer:而不是创建您自己的复制方法。initWithLayer:明确用于创建“层的影子副本,例如,用于presentationLayer方法”。

您可能还需要创建子图层的副本。我一时记不起initWithLayer:这是否适合你。但这initWithLayer:就是核心动画的工作原理,因此它针对此类问题进行了优化。