renderInContext不支持掩码



我有一个相机应用程序加载照片到一个设备与掩码。一切都很好。当我尝试使用renderInContext将视图保存为图像时,我只看到没有任何蒙版的图像。

UIGraphicsBeginImageContext(contentView.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[contentView.layer renderInContext:context];
UIImage *outImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(outImage, self, @selector(image: didFinishSavingWithError: contextInfo:), context);

我读过苹果的一些论文,上面说renderInContext不支持蒙版和合成。我在互联网上做了一些搜索,以获得UIView需要首先作为上下文绘制的信息,然后使用renderInContext来保存图像。

现在我的问题是做这项工作的方法是什么?那么drawRect, drawwinrect, drawLayer, drawwincontent,或者其他方法呢?谁能给我点提示吗?非常感谢。

我从这里开始:http://chinkisingh.com/2013/03/03/draw-on-iphoneipad-screen-using-bezier-paths-core-graphics-ios-app-development/

我有一个UIBezierPath,我想给它应用一个渐变,并应用到一个现有的图像,看看下面的代码是否有帮助

    CGRect r = CGRectMake(0, 0, HEART_SIZE, HEART_SIZE);
    UIBezierPath *heart = [Bezier heartShape:r]; //this is only in my case
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    UIColor *darkPink = [UIColor colorWithHue:0.915 saturation:1.000 brightness:0.941 alpha:1.000];
    UIColor *lightPink = [UIColor colorWithHue:0.917 saturation:0.647 brightness:1.000 alpha:1.000];
    NSArray *gradientColors = [NSArray arrayWithObjects:
                               (id)darkPink.CGColor,
                               (id)lightPink.CGColor,
                               (id)darkPink.CGColor, nil];
    CGFloat gradientLocations[] = {0, 0.5, 1};
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);
    CGColorSpaceRelease(colorSpace);

    UIGraphicsBeginImageContext(r.size);
    heart.lineCapStyle = kCGLineCapRound;
    heart.lineWidth = 10.0f;
    [[UIColor blackColor] setStroke];
    [[UIColor redColor] setFill];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    [heart stroke]; //removed the black stroke around
    [heart fill];
    CGContextAddPath(context, heart.CGPath);
    CGContextClip(context);
    CGContextDrawLinearGradient(context, gradient, CGPointMake(10, 10), CGPointMake(210, 210), kCGGradientDrawsAfterEndLocation); //check that gradient is drawn in the right place
    CGGradientRelease(gradient);
    UIImage *theRightImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

最新更新