Cocoa Touch:呈现图像时忽略蒙版:renderInContext:UIGraphicsGetCurrentContext()

时间:2013-04-25 09:14:53

标签: ios cocoa-touch rendering mask uibezierpath

我正在尝试使用renderInContext:UIGraphicsGetCurrentContext()渲染蒙面视图。我正在掩盖一个白色的视图,这是一个灰色视图的子视图。这适用于显示:我最终在灰色背景下使用白色形状,但是当我渲染时,遮罩被忽略,并且我在生成的图像中看到的只是白色。

UIView子类中的掩码

- (void)drawRect:(CGRect)rect
{
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        CGMutablePathRef patheToMask = CGPathCreateMutable();
        CGRect scrollMaskRect = CGRectMake(30, 30, 100, 100);
        UIBezierPath *scrollMask = [UIBezierPath bezierPathWithRoundedRect:scrollMaskRect cornerRadius:30];
        CGPathAddPath(patheToMask, nil, scrollMask.CGPath);
        [maskLayer setPath:patheToMask];
        CGPathRelease(patheToMask);
        self.layer.mask = maskLayer;
}

渲染代码

-(void)renderImage {
    CGSize renderSize = CGSizeMake(masterView.frame.size.width, masterView.frame.size.height);
    UIGraphicsBeginImageContext(renderSize);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGContextConcatCTM(context, [[masterView layer] affineTransform]);
    [[masterView layer] renderInContext:UIGraphicsGetCurrentContext()];
    renderedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGContextRestoreGState(context);
    UIImageWriteToSavedPhotosAlbum(renderedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    masterView.transform = CGAffineTransformIdentity;
}

这是项目http://owolf.net/uploads/StackOverflow/MaskWeirdness.zip

1 个答案:

答案 0 :(得分:0)

根据文档,renderInContext:UIGraphicsGetCurrentContext()不支持屏蔽。

https://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CALayer_class/Introduction/Introduction.html

来自文档:

  

重要提示:此方法的OS X v10.5实现没有   支持整个Core Animation组合模型。   QCCompositionLayer,CAOpenGLLayer和QTMovieLayer图层不是   渲染。此外,使用3D变换的图层不是   渲染,也不是指定backgroundFilters,过滤器的图层,   compositingFilter,或掩码值。 OS X的未来版本可能会添加   支持渲染这些图层和属性。