Ios 用手画线时裁剪图像
我在视图上添加了一个图像视图。视图使用Ios 用手画线时裁剪图像,ios,Ios,我在视图上添加了一个图像视图。视图使用UIBezierPath和触摸响应器方法在屏幕上拖动后绘制线条。现在我想剪辑图像中线下的部分。我怎样才能做到这一点 我用下面的方法画线 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint p = [touch locationInView:self]; [pat
UIBezierPath
和触摸响应器方法在屏幕上拖动后绘制线条。现在我想剪辑图像中线下的部分。我怎样才能做到这一点
我用下面的方法画线
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path moveToPoint:p];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path addLineToPoint:p]; // (4)
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesMoved:touches withEvent:event];
}
试试这个:
@property (strong, nonatomic) UIBezierPath *bezierPath;
//...
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
//...
self.bezierPath = [UIBezierPath bezierPath];
[self.bezierPath moveToPoint:p];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
//...
[self.bezierPath addLineToPoint:p];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesMoved:touches withEvent:event];
// Here you can call -croppedImage; method and get the image you need.
}
- (UIImage *)croppedImage
{
[self.bezierPath closePath];
UIImage *image = //Your initial image;
CGSize imageSize = image.size;
CGRect imageRect = CGRectMake(0, 0, imageSize.width, imageSize.height);
UIGraphicsBeginImageContextWithOptions(imageSize, NO, [[UIScreen mainScreen] scale]);
// Create the clipping path and add it
[self.bezierPath addClip];
[image drawInRect:imageRect];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return croppedImage;
}
这是一个快速编写的代码,因此可能会出现错误。很抱歉:)是的,它很有效,我刚试过:)唯一的问题是规模。因此,对于start,选择一个大小为(6401136)的图像并放入UIImageView。它不工作。当我使用你的代码,我得到的图像像嗯,有趣。。。这是一个适合我的测试项目:运行我发送给你的项目,然后“画”一个形状(圆形、椭圆形或其他形状),然后看看结果。你似乎不理解我的问题。在画一条线(不是端到端)时,我只想要图像的顶部。你能显示裁剪图像的代码吗?尝试使用此URL裁剪图像并保存到文档目录。我希望这对你有用。@N J Gadhiya我的问题是如何裁剪图像。