我有一个功能,用户可以绘制/着色图像的特定区域,然后将绘制的区域作为裁剪结果。
目前我将绘制坐标存储在一个数组中,并在过程结束时使用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;
}