2013-01-10 4 views
2

그래서 2 CGMutablePathRef를 그릴 수 없을 것 같습니다. 코드는 다음과 같습니다.두 개의 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); 

어떤 생각이 왜 작동하지 않는 이유는 무엇입니까?

+1

작동하지 않습니까? 방법? 너는 무엇을 얻 느냐? –

+0

두 번째 둥근 경로가 표시되지 않습니다. 첫 번째 만 보았습니다. 실제로 이미지 그리기를 제거하면 효과가있었습니다. 궁금한 점은 왜 – adit

답변

0

두 번째 도면을 그리기 전에 클리핑 경로를 재설정하지 않고 첫 번째 rect에 도면을 잘랐습니다. 첫 번째 클리핑 패스가 호출되기 전에 그래픽 상태를 저장해야한다고 생각합니다.

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

다음 두 번째 코드 :

첫 번째 조각은 다음과 같이 될 것이다.

+0

라고 말씀 하시는지 – adit

+0

위의 대답을 일부 코드로 업데이트하여 제가 의미하는 바를 보여줍니다. – user1118321