2

したがって、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);

なぜこれが機能しないのか考えてみてください。

4

1 に答える 1

0

図面を最初の四角形にクリップしましたが、2回目の描画の前にクリッピングパスをリセットしていません。最初のクリッピングパスが呼び出される前に、グラフィックスの状態を保存する必要があると思います。

最初のスニペットは次のようになります。

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

次に、2番目のコードスニペット。

于 2013-01-10T06:48:26.617 に答える