8

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

- (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...
}

但是,当屏幕图像变得复杂(大量视图)时,renderInContext 在 iPad 3 上可能需要 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...
}

这允许界面再次顺利运行,但偶尔会导致在 renderInContext 行上出现 EXC_BAD_ACCESS 崩溃。我尝试首先检查 layer!=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

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

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

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

  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
4

1 回答 1

2

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

您可能还需要创建子图层的副本。我不记得initWithLayer:这是否适合你。但是initWithLayer:核心动画是如何工作的,所以它针对这样的问题进行了优化。

于 2012-08-29T20:43:18.787 回答