【问题标题】:Cocoa Touch: Mask ignored when image is rendered: renderInContext:UIGraphicsGetCurrentContext()Cocoa Touch:渲染图像时忽略掩码:renderInContext:UIGraphicsGetCurrentContext()
【发布时间】:2013-04-25 09:14:53
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: ios cocoa-touch rendering mask uibezierpath


    【解决方案1】:

    根据文档,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 变换的图层不 渲染,也不是指定背景过滤器,过滤器的图层, compositingFilter 或掩码值。 OS X 的未来版本可能会添加 支持渲染这些图层和属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-04
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      • 2012-01-31
      相关资源
      最近更新 更多