Taskinul Haque
Taskinul Haque

Reputation: 724

Assign UIImage to UIImageView between classes

*I need my ViewController to have a copy of the image to hold onto which is disappearing after my PresentViewController is dismissed – *


I have a UIViewController from which I'm calling a presentViewController (modalViewController) with a xib. Inside the xib, I have a UIImageView, which I can markup with a marker (draw onto). My problem is when I dismiss the presentViewController, the variable I set (a UIImage) goes back to nil, and I can't set markup image onto a UIImageView on my main UIViewController.

Drawing code:

UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:drawImage];

UIGraphicsBeginImageContext(drawImage.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0,
                                       drawImage.frame.size.width,
                                       drawImage.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//Savve the drawings on image to a UIImage
savedMarkup = drawImage.image;
lastPoint = currentPoint;

I've tried 2 Methods, A and B, of code to set the Image drawn onto the main UIVC.

Method A works if I remove the line from above UIGraphicsEndImageContext, but if I remove this line, I face many memory warnings and then a crash after some drawing, as expected.

Method A: UIGraphicsBeginImageContextWithOptions(AView.drawImage.bounds.size, NO, 0.0);

[AView.drawImage.image drawInRect:CGRectMake(0, 0,
                                            AView.drawImage.frame.size.width,
                                            AView.drawImage.frame.size.height)];
UIImage *savedMarkup = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

AView.annotatedImage.image = savedMarkup;
[annotation dismissViewControllerAnimated:YES completion:nil];

Method B:

AView.annotatedImage.image = AView.savedMarkup; 

With Method B, if I NSLog the value of AView.savedMarkup - it logs "(null)".

I've been rattling to figure this one out! Your help is very very much appreciated!

Upvotes: 0

Views: 299

Answers (2)

Camo
Camo

Reputation: 1778

take a look of this:

how-to-make-a-simple-drawing-app-with-uikit

It might be useful for you :)

Upvotes: 1

AdamG
AdamG

Reputation: 3718

This issue is likely that the imageView is not instantiated... one way of handling this might be to do lazy instantiation in the presenting view controller class. Another way to do this would be as follows...

Make a public property on the view controller presenting the modal view.

So in the myclass.h file

@property (strong, nonatomic) UIImage *annotatedImage;

Then, before dismissing your view controller write.

(MyClass *) presentingController = (MyClass *) self.presentingViewController
presentingController.annotatedImage = annotatedImage;

And then do what you want in in the completion handler...

[self dismissViewControllerAnimated:YES completion:^{ /* handle here */}];

Or in viewDidAppear:

if (annotatedImage){
   // action here such as allocating the image view and setting the image
 }

And then the presenting view controller will have a copy of the image to hold onto and do what you want with.

EDIT

And of course, make sure your drawing code works.

Upvotes: 1

Related Questions