A: 

Take a look at Overview of Quartz 2D for information on using Quartz 2D on iPhone.

Nick Bedford
i have read that but still unable to do that.it's very complex
Rahul Vyas
+4  A: 

These are the steps:

  1. Create a CGBitmapContext matching the image's colorspace and dimensions.
  2. Draw the image into that context.
  3. Draw whatever you want on top of the image.
  4. Create a new image from the context.
  5. Dispose off the context.

Here's a method that takes an image, draws something on top of it and returns a new UIImage with modified contents:

- (UIImage*)modifiedImageWithImage:(UIImage*)uiImage
{
    // build context to draw in
    CGImageRef image = uiImage.CGImage;
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGContextRef ctx = CGBitmapContextCreate(NULL,
                                             CGImageGetWidth(image), CGImageGetHeight(image),
                                             8, CGImageGetWidth(image) * 4,
                                             colorspace, kCGImageAlphaPremultipliedLast);
    CGColorSpaceRelease(colorspace);

    // draw original image
    CGRect r = CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image));
    CGContextSetBlendMode(ctx, kCGBlendModeCopy);
    CGContextDrawImage(ctx, r, image);
    CGContextSetBlendMode(ctx, kCGBlendModeNormal);

    // draw something
    CGContextAddEllipseInRect(ctx, CGRectInset(r, 10, 10));
    CGContextSetRGBStrokeColor(ctx, 1.0f, 1.0f, 1.0f, 0.5f);
    CGContextSetLineWidth(ctx, 16.0f);
    CGContextDrawPath(ctx, kCGPathStroke);

    CGContextAddEllipseInRect(ctx, CGRectInset(r, 10, 10));
    CGContextSetRGBStrokeColor(ctx, 0.7f, 0.0f, 0.0f, 1.0f);
    CGContextSetLineWidth(ctx, 4.0f);
    CGContextDrawPath(ctx, kCGPathStroke);

    // create resulting image
    image = CGBitmapContextCreateImage(ctx);
    UIImage* newImage = [[[UIImage alloc] initWithCGImage:image] autorelease];
    CGImageRelease(image);
    CGContextRelease(ctx);

    return newImage;
}

To restore to old image, just keep a reference to it.

The cropping thing is not related to the above and you should create a new question for that.

Nikolai Ruhe
I don't know about the accept rate.What does it mean? thanks for your answer.let me check this out
Rahul Vyas
ok your code works great.now tell me when i use quartz for drawing line why it is slow on actual device.and also tell me a way how do i turn image into grayscale.
Rahul Vyas
The accept rate is the percentage of accepted questions. Yours is currently at 27%, which is very low. You should accept more (good) answers or people would not want to help you in the future.
Nikolai Ruhe
Your other questions should be posted as new questions.
Nikolai Ruhe
ok i got it to turn it in gray.now is there a way i can turn the image into something else.where in the above image a selection like circle is there.i mean any path
Rahul Vyas
could you please explain your code in detail.you are such a genious person.
Rahul Vyas
it always turn image into gray.even if i pass a color to it.see the code below.just change your code in some places. // draw original image CGRect r = CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)); CGContextSetBlendMode(ctx, kCGBlendModeCopy); CGContextDrawImage(ctx, r, image); // draw something CGRect rect=CGRectMake(50.0, 50.0, 50.0, 50.0); CGContextAddRect(ctx,rect); CGContextSetRGBStrokeColor(ctx, 0.0f, 1.0f, 1.0f, 0.5f); CGContextSetBlendMode(ctx, kCGBlendModeColor); CGContextFillPath(ctx);
Rahul Vyas
I'm sorry, I don't understand your follow up question.
Nikolai Ruhe
i mean i turn your code as i above posted but it always drwas gray in a give rectangle and redrwas the image.how do draw another colors.in your ellipse.i mean fill elipse with desired color
Rahul Vyas
is there a way we can chat online using google talk.
Rahul Vyas