【问题标题】:renderInContext not support mask?renderInContext 不支持遮罩?
【发布时间】:2013-04-05 14:39:07
【问题描述】:

我有一个相机应用程序可以将照片加载到带面具的设备中。一切都好。当我尝试使用 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 或其他方法呢?谁能给我一个提示。非常感谢。

【问题讨论】:

    标签: ios


    【解决方案1】:

    我从这里开始: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();
    

    【讨论】:

      猜你喜欢
      • 2019-08-06
      • 2023-03-08
      • 2012-11-06
      • 2015-03-23
      • 1970-01-01
      • 2010-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多