gpt4 book ai didi

iOS Core Graphics 如何删除绘图

转载 作者:可可西里 更新时间:2023-11-01 05:35:27 25 4
gpt4 key购买 nike

我正在按照我的涂鸦应用程序教程进行操作。

http://www.raywenderlich.com/18840/how-to-make-a-simple-drawing-app-with-uikit

他的删除功能是使用白色绘图。但是我正在使用图像背景,当我删除时,我真的需要删除。

我试过了

CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);

但它不起作用。这是我的代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
_mouseSwiped = NO;
UITouch *touch = [touches anyObject];
_lastPoint = [touch locationInView:self.tempImageView];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

_mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.tempImageView];

UIGraphicsBeginImageContext(self.tempImageView.frame.size);
[self.tempImageView.image drawInRect:CGRectMake(0, 0, self.tempImageView.frame.size.width, self.tempImageView.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), _lastPoint.x, _lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), _width );


CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), _red, _green, _blue, 1.0);

if (_isErasing) {
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
}
else {
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
}

CGContextStrokePath(UIGraphicsGetCurrentContext());
self.tempImageView.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempImageView setAlpha:_alpha];
UIGraphicsEndImageContext();

_lastPoint = currentPoint;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

CGSize size = self.tempImageView.frame.size;

if(!_mouseSwiped) {
UIGraphicsBeginImageContext(self.tempImageView.frame.size);
[self.tempImageView.image drawInRect:CGRectMake(0, 0, size.width, size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), _width);


CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), _red, _green, _blue, _alpha);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), _lastPoint.x, _lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), _lastPoint.x, _lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
self.tempImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}




UIGraphicsBeginImageContext(self.drawImageView.frame.size);
[self.drawImageView.image drawInRect:CGRectMake(0, 0, size.width, size.height) blendMode:kCGBlendModeNormal alpha:1.0];

if (_isErasing) {
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
}

[self.tempImageView.image drawInRect:CGRectMake(0, 0, self.tempImageView.frame.size.width, self.tempImageView.frame.size.height) blendMode:kCGBlendModeNormal alpha:_alpha];


self.drawImageView.image = UIGraphicsGetImageFromCurrentImageContext();
self.tempImageView.image = nil;
UIGraphicsEndImageContext();
}

最佳答案

通过在触摸开始时交换 tempImageView 和 drawImageView 解决了这个问题,如果它正在删除

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
_mouseSwiped = NO;
UITouch *touch = [touches anyObject];
_lastPoint = [touch locationInView:self.tempImageView];

if (_isErasing) {
self.tempImageView.image = self.drawImageView.image;
self.drawImageView.image = nil;
}

}



- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

_mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.tempImageView];

UIGraphicsBeginImageContext(self.tempImageView.frame.size);
[self.tempImageView.image drawInRect:CGRectMake(0, 0, self.tempImageView.frame.size.width, self.tempImageView.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), _lastPoint.x, _lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), _width );


if (_isErasing) {
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
}
else {
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), _red, _green, _blue, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
}

CGContextStrokePath(UIGraphicsGetCurrentContext());
self.tempImageView.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempImageView setAlpha:_alpha];
UIGraphicsEndImageContext();

_lastPoint = currentPoint;
}





- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

CGSize size = self.tempImageView.frame.size;

if(!_mouseSwiped) {
UIGraphicsBeginImageContext(self.tempImageView.frame.size);
[self.tempImageView.image drawInRect:CGRectMake(0, 0, size.width, size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), _width);


if (_isErasing) {
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
}
else {
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), _red, _green, _blue, _alpha);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
}

CGContextMoveToPoint(UIGraphicsGetCurrentContext(), _lastPoint.x, _lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), _lastPoint.x, _lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
self.tempImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}




UIGraphicsBeginImageContext(self.drawImageView.frame.size);
[self.drawImageView.image drawInRect:CGRectMake(0, 0, size.width, size.height) blendMode:kCGBlendModeNormal alpha:1.0];
[self.tempImageView.image drawInRect:CGRectMake(0, 0, size.width, size.height) blendMode:kCGBlendModeNormal alpha:_alpha];
self.drawImageView.image = UIGraphicsGetImageFromCurrentImageContext();
self.tempImageView.image = nil;
UIGraphicsEndImageContext();
}

关于iOS Core Graphics 如何删除绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22131762/

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