Passing image from one view to another

Discussion in 'iOS Programming' started by aceiswild, Nov 22, 2011.

  1. aceiswild macrumors newbie

    Joined:
    Nov 15, 2011
    Location:
    Canada
    #1
    Hey guys,
    So i have an application that when you push a button it opens the camera and you can take a picture. After you take the picture you can click use and it displays the picture in the same view in a UIImageView.
    What i need to do is when i click "use" i want the image to be sent to a different viewController to view the image. My problem is right now it is not displaying in my new viewController and is just showing a white screen.
    I don't think i can use a UIImageView to display the image in my new viewController because once the image is sent it is programatically broken up into puzzle pieces that you can move around.
    Any suggestions will help and will provide my code if necessary.

    thanks in advance!!
    Steve!
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Providing the code that is called when you click "Use" would probably be a good start.

    Also, how are you planning on breaking up the image into pieces? That might help you decide how to pass the image to the new view controller.
     
  3. aceiswild, Nov 22, 2011
    Last edited: Nov 22, 2011

    aceiswild thread starter macrumors newbie

    Joined:
    Nov 15, 2011
    Location:
    Canada
    #3
    Re:

    Hi Dejo,

    I mean when i click the use button in the camera view.
    I already have the code to break up my image. I know i have to be missing something in my xib. I can explain it easier by showing you my whole code. I hope its not to hard to understand but ask my any questions. So far there are no errors in my project either. Just need to figure out how to transfer a image from one view to another.

    Here is a link to my project zipped up: http://www.mediafire.com/?ejjq494x53jzbgg

    Thank you Lots!!
    Steve
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    So, you probably have some code inside the delegate method for this action, right? Let's see it.

    Dumping your whole project is not really explaining it, unless it's heavily documented. It would be easier for us to diagnose if you just include the relevant code snippets.
     
  5. aceiswild, Nov 22, 2011
    Last edited: Nov 22, 2011

    aceiswild thread starter macrumors newbie

    Joined:
    Nov 15, 2011
    Location:
    Canada
    #5
    Sorry dejo, i thought it might have been easier to show you my whole project to get a better idea for what i ma trying to do. Ill do my best here! Bare with me!!

    Here is my main viewController code that opens the camera or photo gallery and gets the picture and places in a UIImageView.

    viewController .M
    Code:
    -(IBAction) getPhoto:(id) sender {
    	UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    	picker.delegate = self;
    	
    	if((UIButton *) sender == choosePhotoBtn) {
    		picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            
    	} else {
    		picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    	}
    	
    	[self presentModalViewController:picker animated:YES];
    }
    
    
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    	[picker dismissModalViewControllerAnimated:YES];
    
        initPuzzle.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
    }
    
    In my SliderController .M i have the following code which is supposed to retrieve the image from viewController .M and display it in my SliderController view.

    SliderController .M
    Code:
    - (void)viewDidaLoad {
    	//self.view.backgroundColor = [UIColor grayColor];
    	
    	self.tiles = [[NSMutableArray alloc] init];
    	
    	[self initPuzzle:@"UIImagePickerControllerOriginalImage"];
    
        //  self.initPuzzle.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
        
        [super viewDidLoad];
    }
    
    I can't seem to take the image from viewController and place it in SliderController view. I was thinking there is something i have to change in this line of code to send the image to SliderController.M:
    Code:
    initPuzzle.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    I imported viewController.m in SliderController. I know i could probably use another UIImageView in my SliderController to show the picture but if i did that i would not be able to break the picture up into puzzle piece and slide them around. I know my slider code works because i have used it in another app. I was thinking i might have to use loadView to programatically create a NIB instead of using viewDidlaod.

    I hope this can help as a start.

    Thank you!!
    Steve
     
  6. nickculbertson macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #6
    Code:
    - (void)viewDidaLoad {
    There is one problem.
     
  7. aceiswild thread starter macrumors newbie

    Joined:
    Nov 15, 2011
    Location:
    Canada
    #7
    Thanks for noticing! Must have been an error when making a copy from my backup file. Still working on my other issues now!
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    How are you presenting SliderController? Modally or pushed onto the nav stack? Where's the code for that?

    A usual approach, similar to what you're already doing in getPhoto:, is to instantiate your viewController, set a property to the desired value and then present it. As in:
    Code:
    SliderController *sliderController = [[SliderController alloc] init];
    sliderController.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
    [self presentModalViewController:sliderController animated:YES];
    Your SliderController code would then do what it needs to do with the image, usually in viewDidLoad or viewWillAppear.
     
  9. aceiswild, Nov 22, 2011
    Last edited: Nov 22, 2011

    aceiswild thread starter macrumors newbie

    Joined:
    Nov 15, 2011
    Location:
    Canada
    #9

    SliderController has a NIB of its own. Once i get the image from the photo album or camera i also have a button in my viewController that says "Play", when i click that play button it goes to my SliderViewController view where the image should then be displayed. Im going to give your given code a try, just have to change some things around.

    My play button code which goes to SliderController.M is the same as yours except when i add the following code i get an error that i need "]"
    Code:
    sliderController.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 

    Thanks dejo gonna start playing around now.
     
  10. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #10
    Dejo helped me with a problem I was having which is kind of similar. I gathered client information in one ViewController and then pushed a new ViewController on screen to use the information. To solve this problem I had the first ViewController write everything to a Property list and then the new ViewController read in the information back in. My first view was a subclass of UITableViewController and my second view was a subclass if UIViewController first I thought I could use inheritance if I made my second view a subclass of the first but had a hard time with the .xib file and the pretty graphics.

    I don't know if this technique would help you but I thought I would mention it.
     
  11. aceiswild thread starter macrumors newbie

    Joined:
    Nov 15, 2011
    Location:
    Canada
    #11

    Thanks larswik, every bit of information is helpful! I will see what i can do with adding info to a property list. Currently i am experiment with NSUserDefaults. So in my first controller i choose or take an image from camera or album which displays it in my first controller in a UIImageView and then click a save button. Then when i click another button called show image, it goes to my second view and shows the image, revelling it from NSUserDefaults with viewDidLoad. I think my second view NIB has to be programatically made and loaded rather then creating an XIB. The image is fullscreen anyways, so programatically making a nib would not be a problem.

    Thanks,
    Steve!
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    Setting properties, storing in NSUserDefaults, using plists. These are all different approaches to a concept known as data persistence. Other approaches include singletons, using the app delegate, Core Data or some other DB-based storage. There have been plenty of threads on this topic; search for 'data persistence' or 'sharing values' in this forum and you'll find lots of previous threads.
     
  13. aceiswild thread starter macrumors newbie

    Joined:
    Nov 15, 2011
    Location:
    Canada
    #13
    Thanks demo I'll start looking into data persistence. I know there is more then one approach for what I'm trying to do, what would reccomend to be the best method to pass just an image? Could I save it as a string then call it later?

    Thanks and I wil lhave a look around the forum for data persistence

    Steve
     
  14. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #14
    Depends on how widespread throughout your app you need access to this image. If it's only in these two view controllers, I would say the approach I've already suggested (property in Class B, set property from within Class A) is sufficient and certainly widely used by Apple.

    Why would you save an UIImage as an NSString? NSData would probably be better-suited.
     
  15. aceiswild thread starter macrumors newbie

    Joined:
    Nov 15, 2011
    Location:
    Canada
    #15
    I only have the image displaying in 2 different controllers in my app so it's not widely spread around the app which makes it a bit easier!
     

Share This Page