2013-05-06 115 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 = ... 

你也可以在用戶追蹤形狀(你也可以修改貝塞爾路徑則太)進行計算。