2014-09-08 7 views
0

선 그래프를 그리는 함수를 만들었습니다. 어떻게 선 그래프를 마스크로 사용하여 이미지 위에 놓을 수 있습니까? 이미지가 다른 높이에서 다른 색상을 가지고, 그래서 내가 뭘 원하는 줄 그래프로 마스킹 후, 라인 그래프는 다른 수준에서 다른 색상을 표시합니다. 나를 도와 주셔서 감사합니다.IOS drawure를 사용하여 이미지에 마스크 만들기

- (void)drawLineGraphWithContext:(CGContextRef)ctx 
{ 
    CGContextSetLineWidth(ctx, 2.0); 
    CGContextSetStrokeColorWithColor(ctx, [[UIColor colorWithRed:1.0 green:0.5 blue:0 alpha:1.0] CGColor]); 

    int maxGraphHeight = kGraphHeight - kOffsetY; 

    CGContextBeginPath(ctx); 
    CGContextMoveToPoint(ctx, kOffsetX, kGraphHeight - maxGraphHeight * data[0]); 

    for (int i = 1; i < sizeof(data); i++) 
    { 
     CGContextAddLineToPoint(ctx, kOffsetX + i * kStepX, kGraphHeight - maxGraphHeight * data[i]); 
    } 

    CGContextDrawPath(ctx, kCGPathStroke); 

    CGContextSetFillColorWithColor(ctx, [[UIColor colorWithRed:1.0 green:0.5 blue:0 alpha:1.0] CGColor]); 

    for (int i = 0; i < sizeof(data) - 1; i++) 
    { 
     float x = kOffsetX + i * kStepX; 
     float y = kGraphHeight - maxGraphHeight * data[i]; 
     CGRect rect = CGRectMake(x - kCircleRadius, y - kCircleRadius, 2 * kCircleRadius, 2 * kCircleRadius); 
     CGContextAddEllipseInRect(ctx, rect); 
    } 
    CGContextDrawPath(ctx, kCGPathFillStroke); 


CGPathRef myPath = CGContextCopyPath(ctx); 

CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init]; 
CGPathRef myPath = CGContextCopyPath(ctx); 

shapeLayer.path = myPath; 


_imageView.layer.mask = shapeLayer; 



} 
+0

당신의''CAShapeLayer'는''alloc init '하지 않습니다. Szlosek이 말했듯이 –

+0

. U는 CAShapeLayer를 초기화해야합니다 – croyneaus4u

+0

코드를 수정했으나 아무 일도 없었습니다 .... – JennyProGram

답변

0

이렇게 해보십시오.

1. Put the Image into an ImageView. 
2. Since you already have the Path. And rite now, you will get the last 
    point as `path.currentPoint`. Add a Line to the `bottomRight` then 
    to `bottomLeft` and then to the `startingPoint` of the Path. You 
    will need to store the FirstPoint of the Path Somewhere for this. 
    Also, all the Path drawing should be in reference to the `bounds` of 
    the ImageView. 
3. Create a CAShapeLayer with the path above. `shapeLayer.path = path`. 
4. Set it as mask of the imageView -- imageView.mask = shapeLayer`. 

이렇게 시도하십시오. 건배, 재미있게 보내라.

+0

thx a lot. 저는 처음 3 점을했습니다. 하지만 포인트 4 거기에 오류가 UIIMageView 유형의 개체에 속성 마스크를 찾을 수 없습니다 – JennyProGram

+0

올바른 경로를 만든 다음 imageView의 마스크로 설정하십시오 – croyneaus4u

+0

코드를 편집했지만 작동하지 않습니다 ... 몇 줄 추가됨 함수의 맨 아래에 내가 잘못 했나요? – JennyProGram