绘制一个形状并返回给定一系列CGPoints的区域

时间:2015-10-27 11:41:07

标签: ios objective-c

我搜索了很多,无法找到一个库来执行此操作,并且只在堆栈溢出时发现此代码,但有时返回area = 0,并且它不会绘制,有时当它返回时该区域的像素,不准确 我无法在此代码中进行编辑,因为它不容易 而且我甚至不知道这应该如何绘制,也没有指定一个视图来绘制它 我需要帮助完成这项任务,提前谢谢

viewDidLoad方法中的

  - (void)viewDidLoad {
     [super viewDidLoad];
     pointToPoints = [NSMutableArray new]; // suppose it has values , as in my original code i use touch event to get the CGPoint and that part works well 
     areaOfCurveWithPoints(pointToPoints, pointToPoints.count);
  }
static CGPathRef createClosedPathWithPoints( NSMutableArray *points, size_t count) {
    CGMutablePathRef path = CGPathCreateMutable();
    if(points.count){
        CGPoint origin = ((NSValue *)points[0]).CGPointValue;
        CGPathMoveToPoint (path, NULL, origin.x, origin.y);
        for(NSValue *value in points){
            NSLog(@"%f %f",value.CGPointValue.x, value.CGPointValue.y);
            CGPathAddLineToPoint (path, NULL, value.CGPointValue.x, value.CGPointValue.y);
        }
    }
    CGPathCloseSubpath(path);
    return path;
}

static CGRect integralFrameForPath(CGPathRef path) {
    CGRect frame = CGPathGetBoundingBox(path);
    return CGRectIntegral(frame);
}

static size_t bytesPerRowForWidth(CGFloat width) {
    static const size_t kFactor = 64;
    // Round up to a multiple of kFactor, which must be a power of 2.
    return ((size_t)width + (kFactor - 1)) & ~(kFactor - 1);
}

static CGContextRef createBitmapContextWithFrame(CGRect frame, size_t bytesPerRow) {
    CGColorSpaceRef grayscale = CGColorSpaceCreateDeviceGray();
    CGContextRef gc = CGBitmapContextCreate(NULL, frame.size.width, frame.size.height, 8, bytesPerRow, grayscale, kCGImageAlphaNone);
    CGColorSpaceRelease(grayscale);
    CGContextTranslateCTM(gc, -frame.origin.x, -frame.origin.x);
    return gc;
}

static double areaFilledInBitmapContext(gc) {
    size_t width = CGBitmapContextGetWidth(gc);
    size_t height = CGBitmapContextGetHeight(gc);
    size_t stride = CGBitmapContextGetBytesPerRow(gc);
    uint8_t *pixels = CGBitmapContextGetData(gc);
    uint64_t coverage = 0;
    for (size_t y = 0; y < height; ++y) {
        for (size_t x = 0; x < width; ++x) {
            coverage += pixels[y * stride + x];
        }
    }
    return (double)coverage / UINT8_MAX;
}

static double areaOfCurveWithPoints( NSMutableArray* points, size_t count,bool ism2,UIImageView* drawOn) {
    CGPathRef path = createClosedPathWithPoints(points, count);
    CGRect frame = integralFrameForPath(path);
    size_t bytesPerRow = bytesPerRowForWidth(frame.size.width);
    CGContextRef gc = createBitmapContextWithFrame(frame, bytesPerRow);

    CGContextSetFillColorWithColor(gc, [UIColor redColor].CGColor);
    CGContextAddPath(gc, path);
    CGContextFillPath(gc);
    CGPathRelease(path);

    double area = areaFilledInBitmapContext(gc);
    CGContextRelease(gc);
    NSLog(@"pixels of point to point %f",area );
    return area;
}

1 个答案:

答案 0 :(得分:0)

下面的代码执行我要求的^^

- (void)viewDidLoad {
    [super viewDidLoad];

    shapeView = [[CAShapeLayer alloc] init];
    [shapeView setPath:[self createPath].CGPath];

    [[self.view layer] addSublayer:shapeView];
    //shapeView.content
    shapeView.strokeColor = [UIColor redColor].CGColor; //etc...
    shapeView.fillColor = [UIColor greenColor].CGColor;
    shapeView.lineWidth = 2.0;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (UIBezierPath*)createPath
{
    UIBezierPath* path = [[UIBezierPath alloc]init];
    [path moveToPoint:CGPointMake(100.0, 50.0)];
    [path addLineToPoint:CGPointMake(200.0,50.0)];
    [path addLineToPoint:CGPointMake(200.0, 200.0)];
    [path addLineToPoint:CGPointMake(10.0, 200.0)];
    [path closePath];
    return path;
}