ユーザーが画像の特定の領域を描画/色付けして、描画された領域をトリミング結果として取得できる機能があります。
現在、描画座標を配列内に保存し、プロセスの最後にUIBezierPathとCGContextClipToMaskを使用して画像をトリミングしています。問題は、配列に保存した描画座標からの外部座標のみが必要なことです。外側の座標のみを取得するために 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;
}