iOS - Assign UIImage to UIImageView between classes
I have a UIViewController from which I'm calling a presentViewController
(modalViewControler) with a xib: inside the xib, i have a UIImageView,
which I can markup with a marker (draw onto of) - My problem is when I
dismiss the presentViewController : the variable I set (UIImage) goes back
to null - 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;
Code to set Image drawn onto main UIVC I've tried 2 Methods A, B.
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;
Method B If i NSLog Value of AView.savedMarkup - it returns null :/
I've been rattling to figure this one out ! your help is very very much
appreciated !
No comments:
Post a Comment