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