Iphone 如何在objective-c中对图像进行自定义绘制
首先我想说我是objective-c的新手 直到我知道,我已经建立了一个应用程序,只画一个图像(这是.png图像,有3种颜色:红,绿,蓝)。应用程序在整个图像中绘制线条 我想要的是只画一种颜色的线(例如:红色),即使触摸(鼠标)向上移动其他颜色。(…正如我在互联网上读到的,应该使用洪水填充算法) 你能给我一些关于如何解决这个问题的想法吗 我现在使用的代码是:Iphone 如何在objective-c中对图像进行自定义绘制,iphone,objective-c,flood-fill,Iphone,Objective C,Flood Fill,首先我想说我是objective-c的新手 直到我知道,我已经建立了一个应用程序,只画一个图像(这是.png图像,有3种颜色:红,绿,蓝)。应用程序在整个图像中绘制线条 我想要的是只画一种颜色的线(例如:红色),即使触摸(鼠标)向上移动其他颜色。(…正如我在互联网上读到的,应该使用洪水填充算法) 你能给我一些关于如何解决这个问题的想法吗 我现在使用的代码是: - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { mo
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:self.view];
UIGraphicsBeginImageContext(CGSizeMake(320, 568));
[drawImage.image drawInRect:CGRectMake(0, 0, 320, 568)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 2, 0, 2);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
[drawImage setFrame:CGRectMake(0, 0, 320, 568)];
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
[self.view addSubview:drawImage];
}
thnx提前。您可以创建从
UIView
派生的自定义类,并覆盖消息drawRect:
。然后将绘图代码放入drawRect:
。将此自定义类作为子视图添加到UIImageView
上方
#import <QuartzCore/QuartzCore.h>
@interface YourView : UIView {
...
}
in .m file
- (void)drawRect:(CGRect)rect{
CGContextRef ctx = UIGraphicsGetCurrentContext();
//your drawings
}
#导入
@界面视图:UIView{
...
}
在.m文件中
-(void)drawRect:(CGRect)rect{
CGContextRef ctx=UIGraphicsGetCurrentContext();
//你的画
}
提供了一些关于如何“打开”图像和访问像素的线索。CGContextRef context=UIGraphicsGetCurrentContext()代码>将使其更具可读性