iOS核心图形如何擦除绘图

iOS核心图形如何擦除绘图,第1张

概述我正在关注我的涂鸦应用程序的本教程. http://www.raywenderlich.com/18840/how-to-make-a-simple-drawing-app-with-uikit 他的擦除功能是使用白色绘图.但我正在使用图像背景,当我擦除时,我真的需要擦除. 我试过了 CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGB 我正在关注我的涂鸦应用程序的本教程.

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,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,size.wIDth,size.height)];        CGContextSetlineCap(UIGraphicsGetCurrentContext(),kCGlineCapRound);        CGContextSetlinewidth(UIGraphicsGetCurrentContext(),_wIDth);        CGContextSetRGBstrokecolor(UIGraphicsGetCurrentContext(),_Alpha);        CGContextMovetoPoint(UIGraphicsGetCurrentContext(),_lastPoint.y);        CGContextAddlinetoPoint(UIGraphicsGetCurrentContext(),_lastPoint.y);        CGContextstrokePath(UIGraphicsGetCurrentContext());        CGContextFlush(UIGraphicsGetCurrentContext());        self.tempImageVIEw.image = UIGraphicsGetimageFromCurrentimageContext();        UIGraphicsEndImageContext();    }    UIGraphicsBeginImageContext(self.drawImageVIEw.frame.size);    [self.drawImageVIEw.image drawInRect:CGRectMake(0,size.height) blendMode:kCGBlendModenormal Alpha:1.0];    if (_isErasing) {        CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear);    }    [self.tempImageVIEw.image drawInRect:CGRectMake(0,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,_wIDth );    if (_isErasing) {        CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear);    }    else {        CGContextSetRGBstrokecolor(UIGraphicsGetCurrentContext(),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,_wIDth);        if (_isErasing) {            CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear);        }        else {            CGContextSetRGBstrokecolor(UIGraphicsGetCurrentContext(),_Alpha);            CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModenormal);        }        CGContextMovetoPoint(UIGraphicsGetCurrentContext(),size.height) blendMode:kCGBlendModenormal Alpha:1.0];    [self.tempImageVIEw.image drawInRect:CGRectMake(0,size.height) blendMode:kCGBlendModenormal Alpha:_Alpha];    self.drawImageVIEw.image = UIGraphicsGetimageFromCurrentimageContext();    self.tempImageVIEw.image = nil;    UIGraphicsEndImageContext();}
总结

以上是内存溢出为你收集整理的iOS核心图形如何擦除绘图全部内容,希望文章能够帮你解决iOS核心图形如何擦除绘图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1055266.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存