gpt4 book ai didi

objective-c - CGContextClearRect 在 objective-c 中的 touchesMoved 事件中不能正常工作

转载 作者:太空狗 更新时间:2023-10-30 03:51:07 26 4
gpt4 key购买 nike

我使用了这段代码,它部分有效,但图像裁剪不正确 output seen like this

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:_img.superview];

UIGraphicsBeginImageContext(self.img.frame.size);
[_img.image drawInRect:CGRectMake(0, 0, _img.frame.size.width, _img.frame.size.height)];

CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10);
CGContextBeginPath(UIGraphicsGetCurrentContext());

CGContextClearRect (UIGraphicsGetCurrentContext(), CGRectMake(currentPoint.x, currentPoint.y, 15, 15));
CGContextStrokePath(UIGraphicsGetCurrentContext());
_img.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}

最佳答案

我之前遇到了同样的问题,然后我尝试了下面的代码,它对我有用

UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.img_main];
// NSLog(@"Current ponint : %f,%f",currentPoint.x,currentPoint.y);

self.img_main.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.img_main.contentMode = UIViewContentModeScaleAspectFit;

UIGraphicsBeginImageContextWithOptions(self.img_main.bounds.size, NO, 0.0);
[_img_main.image drawInRect:CGRectMake(0,0, self.img_main.bounds.size.width,self.img_main.bounds.size.height)];
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);

//For clearing Size and Mode
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush_Size );
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDestinationOut);
//For Clearing Opacity
CGContextSetAlpha(UIGraphicsGetCurrentContext(), brush_opacity);
CGContextStrokePath(UIGraphicsGetCurrentContext());

_img_main.image = UIGraphicsGetImageFromCurrentImageContext();

CGContextFlush(UIGraphicsGetCurrentContext());
UIGraphicsEndImageContext();

关于objective-c - CGContextClearRect 在 objective-c 中的 touchesMoved 事件中不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37108755/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com