0

In my application I draw a rectangle on an image view with my finger. The rectangle is created by using the drawRect method and added on that image view.

Now my issue is that I have to store that image along with those rectangles in photolibrary. Like an image with rectangles having on imageview.

Can anyone help me in finding how to do this?

Here is my code for drawRect method:

-(void)drawRect:(CGRect)rect 
{
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(contextRef, 5.0);
    CGContextSetRGBStrokeColor(contextRef, 255.0, 1.0, 0.0, 1.0);
    CGContextSetStrokeColorWithColor(contextRef,[UIColor redColor].CGColor);

    // Draw the border along the view edge.
    CGContextStrokeRect(contextRef, rect); 
}
remio
  • 1,242
  • 2
  • 15
  • 36
Naresh
  • 1
  • 4
  • How do you draw your rectangle? can you show the code here? – Bazinga Jul 03 '12 at 11:50
  • -(void)drawRect:(CGRect)rect { CGContextRef contextRef = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(contextRef, 5.0); CGContextSetRGBStrokeColor(contextRef, 255.0, 1.0, 0.0, 1.0); CGContextSetStrokeColorWithColor(contextRef,[UIColor redColor].CGColor); // Draw the border along the view edge CGContextStrokeRect(contextRef, rect); } } – Naresh Jul 04 '12 at 05:55
  • in this way i create the rectangle and add this view on imageview by using addsubview – Naresh Jul 04 '12 at 05:56
  • @Naresh - there is an 'edit' button on questions which enables the question owner to edit and update the question. This is good for making clarifications and especially for posting code - since code in these little comments is very hard to read. Cheers! – spring Jul 04 '12 at 14:13

1 Answers1

0

The method parameter view is the view on which both image and drawing are added. Calling this method will return an UIImage.

-(UIImage*)convertViewToImage:(UIView*)view
{ 
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], CGRectMake(0, 0, view.frame.size.width, view.frame.size.height));
UIImage *targetImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

return targetImage;
} 
Warif Akhand Rishi
  • 23,920
  • 8
  • 80
  • 107