0

我有一个相机应用程序可以将照片加载到带面具的设备中。一切都好。当我尝试使用 renderInContext 将视图保存到图像时,我只看到没有任何遮罩的图像。

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

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

现在我的问题是用什么方法来完成这项工作?drawRect、drawInRect、drawLayer、drawInContent 或其他方法呢?谁能给我一个提示。十分感谢。

4

1 回答 1

0

我从这里开始: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();
于 2013-07-08T22:29:20.437 回答