Question

i have a view with uiimageview i assign this uiimageview image by camera..now i want to do some drawing onto image....using coregraphics.i want to do something like this... select an area by touching and drawing line when line joins something like circle or any shape..i want to change that particular area in to something else for example change color there.turn that into grayscale.. till now i am able to draw line...here is an image of line drawn over a uiimage view...

alt text

but i am unable to figure it out how do i draw at imageview's image..mean how to modify imageview's image???

also i want to restore image when click on clear button or something like undo..does someone knows how to achieve this?

and

how do i create a rectangle when click on crop button move the rectangle any where on the screen...and then push the button to crop the image...and then save cropped image..

Was it helpful?

Solution

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.

OTHER TIPS

A lot easier solution would be

(UIImage *) modifyImage:(UIImage *)inputImage
{
   UIGraphicsBeginImageContext(inputImage.size);
   [inputImage drawInRect:CGRectMake(0, 0, inputImage.size.width, inputImage.size.height);
   CGContextRef ctx = UIGraphicsGetCurrentContext();
   //Drawing code using above context goes here
   /*
    *
    */
   UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   return outputImage;
}

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top