在客观c中画出一个人体

时间:2014-06-23 12:09:10

标签: ios objective-c quartz-core

我需要在我的一个iphone应用程序中绘制一个人体。任何人都可以帮助我

我正在尝试使用“drawInContext”但不知道如何绘制它

下面是我想要的应用程序图片 I want to draw like in this image

以下是我正在尝试的代码,它只显示了人体的线条

- (void)drawInContext:(CGContextRef)context
{    
int lineWidth = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) ? 2 : 10;

CGContextSetLineWidth(context, lineWidth);

//At this point we have a NSArray filled with at least 1 NSValue
//NSLog(@"Number of Segments %d", _jointSkeletonSegments.count);

for (NSValue *ourPointNSValue in _jointSkeletonSegments)
{

    struct SkeletonSegment ourPoint;

    [ourPointNSValue getValue:&ourPoint];

    //Draw the lines
    [self drawLineWithCGPoint:ourPoint.startPosition
             andSecondCGPoint:ourPoint.endPosition
                  withContext:context];

    //Draw the points
    [self drawPointWithCGPoint:ourPoint.startPosition withContext:context];
    [self drawPointWithCGPoint:ourPoint.endPosition withContext:context];
}
}

- (void)drawLineWithCGPoint:(CGPoint)startPoint andSecondCGPoint:(CGPoint)endPoint withContext:(CGContextRef)context
{
//Decide what color we want
UIColor *ourColor = [UIColor greenColor];

//Draw
CGContextSaveGState(context);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetStrokeColorWithColor(context, ourColor.CGColor);
CGContextMoveToPoint(context, startPoint.x + 0.5, startPoint.y + 0.5);
CGContextAddLineToPoint(context, endPoint.x + 0.5, endPoint.y + 0.5);
CGContextStrokePath(context);
CGContextRestoreGState(context);
}

- (void)drawPointWithCGPoint:(CGPoint)point withContext:(CGContextRef)context
{
CGRect rect = CGRectZero;
int pointSize = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) ? 10 : 20;

CGContextSaveGState(context);

//Make the new rect in the center of the coordinate.
rect = CGRectMake(point.x - pointSize/2,
                  point.y - pointSize/2,
                  pointSize,
                  pointSize);

CGContextStrokeEllipseInRect(context, rect);
CGContextFillEllipseInRect(context, rect);

CGContextRestoreGState(context);
}

谢谢, Rafeeq

2 个答案:

答案 0 :(得分:5)

您是否看过应用程序Paint Code它是一个绘图包,允许您绘制任何图像然后它编写目标C代码来绘制它!

paint code app in action

答案 1 :(得分:2)

您可以使用UIBezierPath绘制形状。创建封闭路径后,填充颜色。 您可以使用此链接进行参考

https://developer.apple.com/library/ios/documentation/2ddrawing/conceptual/drawingprintingios/BezierPaths/BezierPaths.html