我有一個功能,用戶可以繪製/着色圖像的特定區域,然後將繪製區域作爲裁剪結果。獲取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