Irfan DANISH
Irfan DANISH

Reputation: 8489

iPhone free hand drawing over image and save high quality edited image

I am successfully able to draw on UIView e.g. drawScreen with

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

I have added my UIImageView as subview of above UIView e.g. drawScreen so that it looks you are drawing over the image.

Now i want to save image with drawing, i have also done this part using

UIGraphicsBeginImageContext(CGSizeMake(drawScreen.frame.size.width, drawScreen.frame.size.height));

Problem:
The problem is that the resulted save image is of size drawScreen and my original image is of good quality. e.g,

size of drawScreen is 300x300

The size of my original image is 640x640

The size of resulted image is also 300x300.

How can i save original size image with editing?

Upvotes: 0

Views: 428

Answers (2)

Irfan DANISH
Irfan DANISH

Reputation: 8489

I am able to save original sized image with editing using UIScrollView. I have disabled single finger scroll in my UIScrollView only allowed two fingers scrolling and use single finger to draw over image. I have used following codes to save original size image

UIGraphicsBeginImageContext(self.scrollView.contentSize);
 {
     CGPoint savedContentOffset = self.scrollView.contentOffset;
     CGRect savedFrame = self.scrollView.frame;

     self.scrollView.contentOffset = CGPointZero;
     self.scrollView.frame = CGRectMake(0, 0, self.scrollView.contentSize.width, self.scrollView.contentSize.height);

     [self.scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
     UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
     UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
     self.scrollView.contentOffset = savedContentOffset;
     self.scrollView.frame = savedFrame;
 }
 UIGraphicsEndImageContext();

Upvotes: 0

WolfLink
WolfLink

Reputation: 3317

Start by simply replacing drawScreen.frame.size.width and drawScreen.frame.size.height with the size of the original image.

First of all, you will need to create a new UIView instance that is the size of your image (and add your image to it). Then you will need to scale and redraw your art to it. Depending on how you are storing your image, you will either need to multiply your coordinates by a calculated factor, or render something and then enlarge it. Either way, you will add your enlarged graphics as a subview on top of your new UIView. Then use the UIGraphicsGetCurrentImageContext.

You will want to read this documentation.

Also, you may find this question somewhat helpful.

Upvotes: 1

Related Questions