2013-05-06 85 views
1

我有一个功能,用户可以绘制/着色图像的特定区域,然后将绘制区域作为裁剪结果。获取CGPoint的边缘/外部坐标

enter image description here

目前我存储阵列内的拉伸坐标,并且在该过程结束时,我使用UIBezierPathCGContextClipToMask裁剪图像。问题是我只需要从我存储在数组中的绘制坐标的外部坐标。有什么办法来过滤CGPoints只获得外部坐标?

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    // add the first coordinate 
    [points addObject:[NSValue valueWithCGPoint:lastPoint]]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    // add more coordinates as finger moves 
    [points addObject:[NSValue valueWithCGPoint:currentPoint]]; 

} 

- (void) crop { 


    CGRect rect = CGRectZero; 
    rect.size = self.mainImage.image.size; 

    UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0.0); 

    { 

     [[UIColor blackColor] setFill]; 
     UIRectFill(rect); 
     [[UIColor whiteColor] setFill]; 

     UIBezierPath * beziPath = [UIBezierPath bezierPath]; 

     NSValue * firstValue = [points objectAtIndex:0]; 
     CGPoint firstPoint = firstValue.CGPointValue; 
     [beziPath moveToPoint:[ARCroppingViewController 
         convertCGPoint:firstPoint fromRect1:self.mainImage.frame.size 
         toRect2:self.mainImage.image.size]]; 

     for (uint i = 1; i < points.count; i++) { 


      NSValue * value = [points objectAtIndex:i]; 
      CGPoint point = value.CGPointValue; 
      NSLog(@"point: %@", NSStringFromCGPoint(point)); 
      [beziPath addLineToPoint:[ARCroppingViewController 
          convertCGPoint:point fromRect1:self.mainImage.frame.size 
          toRect2:self.mainImage.image.size]]; 

      } 

    [beziPath closePath]; 
    [beziPath fill]; 


     } 


    UIImage *mask = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0); 
    { 

     CGContextClipToMask(UIGraphicsGetCurrentContext(), rect, mask.CGImage); 
     [self.mainImage.image drawAtPoint:CGPointZero]; 

    } 

    UIImage *maskedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    NSLog(@"mask image: %@", NSStringFromCGSize(maskedImage.size)); 
    self.mainImage.image = maskedImage; 

} 

回答

0

为了获得边缘,我会保存已经处于保存阶段的极端值(将点添加到数组中)。所以保持最高和最低的x和y值。认为它也比滤波阵列:)

编辑

int xMin, xMax, yMin, yMax; 
-(void)resetValues 
{ 
    xMax = yMax = 0; 

    xMin = yMin = self.mainImage.image.size.width; 
} 

- (void)checkForExtremes:(CGPoint)point 
{ 
    // Have my IDE not here for checking the MIN() and MAX(), so using this way :P 
    lastPoint.x > xMax ? xMax = lastPoint.x : nil; 
    lastPoint.x < xMin ? xMin = lastPoint.x : nil; 
    lastPoint.y > yMax ? yMax = lastPoint.y : nil; 
    lastPoint.y < yMin ? yMin = lastPoint.y : nil; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    // add the first coordinate 
    [points addObject:[NSValue valueWithCGPoint:lastPoint]]; 

    [self checkForExtremes:lastPoint]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    // add more coordinates as finger moves 
    [points addObject:[NSValue valueWithCGPoint:currentPoint]]; 
    [self checkForExtremes:currentPoint]; 
} 

BTW容易,结果是那么

cut Image

+0

如何获得极值? – ariauakbar 2013-05-06 15:14:41

+0

检查我的编辑:) – geo 2013-05-06 16:05:46

+0

谢谢,我会试试你的答案。但是我需要一个精确的圆形裁剪结果(与绘图路径相同) – ariauakbar 2013-05-06 17:11:34

0

就遍历所有的点(你现在的样子)但检查每个点并存储x和y值的最大值和最小值。然后在循环结束时,您可以在(现在正方形)所选区域的角落获得4个点。

CGFloat minX, maxX, minY, maxY; 

for (... { 
    minX = MIN(minX, point.x); 
    maxX = ... 
} 

CGPoint topLeft = CGPointMake(minX, minY); 
CGPoint topRight = ... 

你也可以在用户追踪形状(你也可以修改贝塞尔路径则太)进行计算。