我有一个功能,用户可以绘制/着色图像的特定区域,然后将绘制区域作为裁剪结果。获取CGPoint的边缘/外部坐标
目前我存储阵列内的拉伸坐标,并且在该过程结束时,我使用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;
}
如何获得极值? – ariauakbar 2013-05-06 15:14:41
检查我的编辑:) – geo 2013-05-06 16:05:46
谢谢,我会试试你的答案。但是我需要一个精确的圆形裁剪结果(与绘图路径相同) – ariauakbar 2013-05-06 17:11:34