2

I have this code:

-(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;
}

with this code I colour in a view with a black line, but I want to colour with a specific png (as a brush for example); and have a particular effect; what changes should I do?

4

4 に答える 4

2

イメージ内の CGPoint から色を取得するメソッドを作成しました: ImageOperations.h:

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

ImageOperations.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

png のビットマップが単一の色しか持たない場合は、その rgb 値を設定できます。それ以外の場合は、パスのように png 自体を描画したい場合は、ポイントを使用して線を描画するために使用されるのと同じ手法を使用します。これを実現するには、png 画像を配置します。ここでは、単一の png がポイントとして動作することを意味します。

于 2012-03-21T10:09:16.657 に答える
0

パターンの色またはストロークのパターンを使用できます。色のパターンを設定するには:

画像のサイズが 320x480であることを確認してください

CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:myimage].CGColor);
于 2012-06-21T07:21:47.650 に答える
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 に答える