2

我有这个代码:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;

UITouch *touch = [touches anyObject];   
CGPoint currentPoint = [touch locationInView:drawImage];

UIGraphicsBeginImageContext(drawImage.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 15.0);

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 0, 0, 1.0); //black

CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

lastPoint = currentPoint;
}

使用此代码,我用黑线在视图中着色,但我想用特定的 png 着色(例如,作为画笔);并具有特定的效果;我应该做些什么改变?

4

4 回答 4

2

我编写了一个从图像中的 CGPoint 获取颜色的方法:ImageOperations.h:

+ (UIColor *)getColorFromImage:(UIImage*)image atX:(int)x andY:(int)y;

图像操作.m

+ (UIColor *)getColorFromImage:(UIImage*)image atX:(int)x andY:(int)y {
    CGImageRef imageRef = [image CGImage];
    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char *rawData = calloc(height * width * 4, sizeof(unsigned char));
    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;
    CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                                 bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
    CGContextRelease(context);

    int byteIdx = (bytesPerRow * y) + x * bytesPerPixel;

    CGFloat red   = (rawData[byteIdx]     * 1.0) / 255.0;
    CGFloat green = (rawData[byteIdx + 1] * 1.0) / 255.0;
    CGFloat blue  = (rawData[byteIdx + 2] * 1.0) / 255.0;
    CGFloat alpha = (rawData[byteIdx + 3] * 1.0) / 255.0;
    byteIdx += 4;

    UIColor *acolor = [UIColor colorWithRed:red/255.f
                                      green:green/255.f
                                       blue:blue/255.f    
                                      alpha:alpha/255.f];      
    free(rawData);

    return acolor;
}

方法调用如下所示:

UIColor *myColor= [ImageOperations getColorFromImage:myImage atX:cgPoint.x andY:cgPoint.y];

self.myView.backgroundColor = myColor;

希望这可以帮助。

于 2012-03-21T12:24:23.990 回答
0
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, alpha);
in your code  u set r g b -->0
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 0, 0, 1.0); 

that why its coming in black color, u need to set some value e.g
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 20, 50, 10, 1.0); 
于 2012-03-21T11:51:19.537 回答
0

您可以使用图案颜色或描边图案来设置颜色图案:

请确保图片为 ​​320x480 尺寸

CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:myimage].CGColor);
于 2012-06-21T07:21:47.650 回答
0

如果您的 png 位图只有单一颜色,那么您可以设置该 rgb 值。否则,如果您想像路径一样绘制 png 本身,请使用与使用点绘制线相同的技术..继续放置点的集合将给您线尝试通过放置 png 图像来实现这一点。我的意思是,你的单个 png 表现为点。

于 2012-03-21T10:09:16.657 回答