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?

cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241

4 Answers4

2

I wrote a method to get a color from a CGPoint in an image: 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;
}

The method call looks like this:

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

self.myView.backgroundColor = myColor;

Hope this helps.

LJ Wilson
  • 14,445
  • 5
  • 38
  • 62
  • no you don't understand my problem; i want color in a view as if I use a brush instead a simple point!!! – cyclingIsBetter Mar 21 '12 at 14:40
  • Your question is very vague and there is no reason to get upset. What I understood was that you are trying to get the color from a png (image) and use that color to set the background color for a UIView. If that isn't correct, please edit your question. – LJ Wilson Mar 21 '12 at 15:49
  • I have posted an example that show how to color with a simply line, then I want to color with a png – cyclingIsBetter Mar 21 '12 at 16:49
0

You can use pattern color or stroke pattern To set color pattern:

Please make sure that the image is 320x480 size

CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:myimage].CGColor);
The iOSDev
  • 5,237
  • 7
  • 41
  • 78
0

If ur bitmap of png is having only single color then u can set that rgb value.Else if u want to draw png itself like path use same technique used to draw line using points..Set of continues placing of points wil give u line try to achieve this by placing png images. I mean here ur single png behaves as point.

Allamaprabhu
  • 845
  • 1
  • 7
  • 17
  • I wish i could help u wit some sample..I never tried it before but this was possible solution came into my mind at first look.. – Allamaprabhu Mar 21 '12 at 10:19
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); 
Priyanka Singh
  • 250
  • 2
  • 2