0

I have an PNG image and I want to fill that image with gradient. Is this possible?

Means on the time of touch on iPad that part should be fill with gradient.

Just like in flood fill image fill with simple color(R+G+B).

I want that image fill gradient.

Thanx in advance for help.

And this is my line of code where i am find irregular shape and then fill the color by touch i need to modified it.

- (unsigned char*)rawDataFromImage:(UIImage*)image
{
    CGImageRef imageRef = [image CGImage];

    NSUInteger width = CGImageGetWidth(imageRef);

    NSUInteger height = CGImageGetHeight(imageRef);

    NSLog(@"w=%d,h=%d",width,height);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char *rawData = malloc(height * width * 4);
    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);
    return rawData;
}

If any one having different solution for that so please give me.

I don't want to draw color on image i want to touch and fill on image but with gradient.

virantporwal
  • 989
  • 2
  • 6
  • 26
  • 1
    Welcome to Stack Overflow. When asking "How to X" questions we usually like to know what you have already tried and why that didn't work for you. When supplying additional information to your question we prefer that you *edit* the original question (as opposed to posting comments or answers). – David Rönnqvist Sep 10 '13 at 11:33
  • Actually i read about gradient and i found noting for irregular shapes. In all examples they give a constant shape for gradient but my requirement is different form them.How can i give the coordinates for irregular shapes.That is main issue. – virantporwal Sep 10 '13 at 11:40
  • 1
    So have you read about paths? Looking at UIBezierPath or CGPath would be a good place to start learning about irregular shapes – David Rönnqvist Sep 10 '13 at 11:47
  • yes but i didnt get any thing for that. – virantporwal Sep 10 '13 at 11:53
  • 1
    in that case I can suggest the keywords "clip" and "mask" – David Rönnqvist Sep 10 '13 at 12:57
  • @DavidRönnqvist Please suggest me any one link because i new in graphics please. – virantporwal Sep 10 '13 at 13:14
  • Both answers for [this question](http://stackoverflow.com/q/15866179/608157) draw a gradient in a irregular shape (an arc) – David Rönnqvist Sep 10 '13 at 13:15
  • @DavidRönnqvist i read from link,i read about clip and mask also,but i don't want cut my image and i don't want to mask my image.i just want to fill color in my image by touch.just like if i have animal image then i touch on eye then only eye will fill up by color.And because of i am new in ios i don,t find any code for that.so help me for that please. – virantporwal Sep 12 '13 at 05:58
  • Give me solution please.... – virantporwal Sep 12 '13 at 08:13

1 Answers1

0
-(void)drawRect:(CGRect)rect 
{
    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    CGGradientRef glossGradient;
    CGColorSpaceRef rgbColorspace;
    size_t num_locations = 2;
    CGFloat locations[2] = { 0.0, 1.0 };
    CGFloat components[8] = { 1.0, 1.0, 1.0, 0.35,  // Start color
         1.0, 1.0, 1.0, 0.06 }; // End color

    rgbColorspace = CGColorSpaceCreateDeviceRGB();
    glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);

    CGRect currentBounds = self.bounds;
    CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f);
    CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds),   CGRectGetMidY(currentBounds));
    CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, midCenter, 0);

    CGGradientRelease(glossGradient);
    CGColorSpaceRelease(rgbColorspace); 
}
D-eptdeveloper
  • 2,430
  • 1
  • 16
  • 30