1

ユーザーが画像の特定の領域を描画/色付けして、描画された領域をトリミング結果として取得できる機能があります。

ここに画像の説明を入力

現在、描画座標を配列内に保存し、プロセスの最後に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;

}
4

2 に答える 2

0

(現在のように)ポイントをループするだけですが、各ポイントをチェックして、x 値と y 値の最大値と最小値を保存します。次に、ループの最後に、選択した領域 (現在は正方形) の角にある 4 つのポイントを取得できます。

CGFloat minX, maxX, minY, maxY;

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

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

ユーザーがシェイプをトレースしている間にこれらの計算を実行することもできます (ベジェ パスを変更することもできます)。

于 2013-05-06T14:47:42.557 に答える
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];
}

ところで、結果は次のとおりです

カット画像

于 2013-05-06T14:45:15.187 に答える