0

を使用してマスクされたビューをレンダリングしようとしています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

4

1 に答える 1

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 の将来のバージョンでは、これらのレイヤーとプロパティのレンダリングのサポートが追加される可能性があります。

于 2013-05-11T02:30:34.640 に答える