Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何在objective-c中对图像进行自定义绘制_Iphone_Objective C_Flood Fill - Fatal编程技术网

Iphone 如何在objective-c中对图像进行自定义绘制

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

首先我想说我是objective-c的新手

直到我知道,我已经建立了一个应用程序,只画一个图像(这是.png图像,有3种颜色:红,绿,蓝)。应用程序在整个图像中绘制线条

我想要的是只画一种颜色的线(例如:红色),即使触摸(鼠标)向上移动其他颜色。(…正如我在互联网上读到的,应该使用洪水填充算法)

你能给我一些关于如何解决这个问题的想法吗

我现在使用的代码是:

- (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()将使其更具可读性