2013-04-25 72 views
0

我試圖使用renderInContext:UIGraphicsGetCurrentContext()渲染蒙面視圖。我掩蓋了一個白色視圖,它是灰色視圖的子視圖。這對於顯示效果很好:我最終在灰色背景下形成白色形狀,但是當我渲染蒙版時會被忽略,並且我在所得圖像中看到的只是白色。Cocoa Touch:渲染圖像時忽略遮罩: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

回答

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可能會添加 支持以渲染這些圖層和屬性。