When I press a button in Class1, I open Class2. Once done in Class2 I take a screenshot, set it to a UIImage, and go back to Class1. I would like to take this UIImage from Class2 and pass it to a Class1 image, but I do not know how to do this.
This is what I have so far...
Code:
//Class2.m
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
[self.view.layer renderInContext:ctx];
UIImage *mapImage = UIGraphicsGetImageFromCurrentImageContext();
//
//This is where I want to set the Class1 UIImage to the mapImage
//
UIGraphicsEndImageContext();
It depends on the relationship between Class1 and Class2 and what each of them are
Code:
//In Class1 you can do this if Class2 doesn't really do anything except make an image
Class2 *c2 = //init your class2 object
self.Image = [c2 GetScreenshotAsUIImage]; //Just return the UIImage you created in the posted method
//Or if there is an actual view that is shown for Class2 this should work
((Class1*)parentViewController).Image = mapImage;
If Class1 and Class2 are unrelated you can use a property in your app delegate
Where is Class2 created? What type of Classes are these? NSObject, UIViewControllers, etc...
Thanks for the quick response!
I added Class2 as a UIViewController subclass with a XIB file. Class2 has a MKMapView and a gesture recognizer. I ultimately want to take this screenshot and set a Class1 button image.
I was trying to implement
Code:
((Class1*)parentViewController).Image = mapImage;
but parentViewController was undeclared. This was very similar to what I was trying...
Yea, i wasn't sure if UIViewControllers had a parentViewController or not. You're trying cast your delegate as Class1 which is incorrect. Cast it to your actual AppDelegate. You'll need to add an image property to your AppDelegate class. Then in Class1 you can get your delegate the same way as in Class2 and grab the Image property from there
When I press a button in Class1, I open Class2. Once done in Class2 I take a screenshot, set it to a UIImage, and go back to Class1. I would like to take this UIImage from Class2 and pass it to a Class1 image, but I do not know how to do this.
This is what I have so far...
Code:
//Class2.m
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
[self.view.layer renderInContext:ctx];
UIImage *mapImage = UIGraphicsGetImageFromCurrentImageContext();
//
//This is where I want to set the Class1 UIImage to the mapImage
//
UIGraphicsEndImageContext();
Better still, use a singleton data container object. I wrote a post on the technique just yesterday:
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.