iphone - 绘制两个CGMutablePath Ref的问题

标签 iphone ios objective-c ipad

因此,我似乎无法绘制2 CGMutablePathRef。这是代码:

CGRect mainRect = CGRectMake(2, 2, rect.size.width-4, 210);
    CGMutablePathRef mainPathRef = createRoundedRectForRect(mainRect, 4);

    if (self.imageExists_){

        [[UIColor colorWithRed:0 green:0 blue:0 alpha:1.0] set];

        CGContextAddPath(context, mainPathRef);
        CGContextClip(context);
        UIGraphicsBeginImageContext(mainRect.size);

        //need to flip the images to that it is drawn appropriately as CALayer uses a different coordinate system
        CGContextSaveGState(context);
        CGContextTranslateCTM(context, 0.0, 210);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextDrawImage(context, mainRect, self.highlightItem_.highlightStoryImage.CGImage);

        UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        [scaledImage drawAtPoint:CGPointMake(0, 0)];
        CGContextRestoreGState(context);

这样就可以在我指定的路径(剪切后)上绘制图像。但是然后我想在其下绘制另一个圆角矩形,所以我这样做了:
 [[UIColor colorWithRed:0 green:0 blue:0 alpha:1.0] set];

     CGFloat colors [] = {
     0.20, 0.20, 0.20, 1.0,
     0.17, 0.17, 0.17, 1.0
     };

     CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB();
     CGGradientRef gradient = CGGradientCreateWithColorComponents(baseSpace, colors, NULL, 2);
     CGColorSpaceRelease(baseSpace), baseSpace = NULL;

     CGContextSaveGState(context);

     CGRect commentRect = CGRectMake(2, 215, rect.size.width-4, rect.size.height - 215);
     CGMutablePathRef pathRef = createRoundedRectForRect(commentRect, 3);

     CGContextAddPath(context, pathRef);
     CGContextClip(context);

     CGPoint startPoint = CGPointMake(CGRectGetMidX(commentRect), CGRectGetMinY(commentRect));
     CGPoint endPoint = CGPointMake(CGRectGetMidX(commentRect), CGRectGetMaxY(commentRect));

     CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
     CGGradientRelease(gradient), gradient = NULL;

     CGContextRestoreGState(context);

     CGContextAddPath(context, pathRef);
     CGContextDrawPath(context, kCGPathStroke);

知道为什么这行不通吗?

最佳答案

您已将图形剪切到第一个矩形,但是在第二次绘制之前尚未重置剪切路径。我相信,在调用第一个剪切路径之前,您需要保存图形状态。

您的第一个代码段应如下所示:

CGContextSaveGState(...);
CGContextAddPath(...);
CGContextClip(...);
UIGraphicsBeginImageContext(...);
... rest of your drawing code...
CGContextRestoreGState(...);

然后是第二个代码段。

关于iphone - 绘制两个CGMutablePath Ref的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14250238/

相关文章:

iphone - 在 Objective C 中创建自定义对象数组

ios - 在 iOS 上逐字显示文本

ios - 将 UIImageView 从正常更改为突出显示不起作用

ios - 单击时avaudioPlayer音频未打开(swift4)

objective-c - iOS 对上传任务进行排队

iphone - 使用 CoreGraphics 绘制内阴影

objective-c - 我应该什么时候移除观察者?在移除观察者之前释放对象时出错

iphone - 如何查看在iphone应用程序中运行的sqlite文件中的数据?

iphone - CATransition 动画键总是 "transition"

iphone - 关闭 ModalViewController 后 UIScrollView 的内容发生了变化