Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Passing image from one view to another

aceiswildaceiswild Posts: 62Registered Users
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.
I can provide my code in advance, but would be ALOT to post here!

thanks in advance!!
Steve!
Post edited by aceiswild on
Don't be a quitter! Never give up and keep pushing to succeed at the things you love!

Replies

  • rocotilosrocotilos Posts: 3,216iPhone Dev SDK Supporter, Registered Users
    You can save it in your app sandbox first upon loading using NSUserDefaults. And then, in the next viewController, retrieve it back out.
  • aceiswildaceiswild Posts: 62Registered Users
    rocotilos;390346 said:
    You can save it in your app sandbox first upon loading using NSUserDefaults. And then, in the next viewController, retrieve it back out.
    Thanks rocotiles, I will give it a shot when I get back from the girlfriends. It sounds pretty logic and straight forward!

    Steve
    Don't be a quitter! Never give up and keep pushing to succeed at the things you love!
  • rocotilosrocotilos Posts: 3,216iPhone Dev SDK Supporter, Registered Users
    aceiswild;390365 said:
    Thanks rocotiles, I will give it a shot when I get back from the girlfriends. It sounds pretty logic and straight forward!

    Steve
    girlfriends

    :eek: plural :D
  • aceiswildaceiswild Posts: 62Registered Users
    rocotilos;390383 said:
    girlfriends

    :eek: plural :D
    haha nothing wrong with more then 1 :p
    She hates me working on my computer!

    So i am guessing since i havnt explored to much with sandbox, when i use NSUserDefaults, i can take or select an image from my album and it will be saved in sandbox, then when i need to get that image in a different viewController, i can just call it from sandbox to retrieve the image and then use it?

    Im gonna start researching this, very interesting and useful solution!

    Thanks!!
    Don't be a quitter! Never give up and keep pushing to succeed at the things you love!
  • rocotilosrocotilos Posts: 3,216iPhone Dev SDK Supporter, Registered Users
    Yes, you are right. Except I think using the app sandbox directly probably is better than using NSUserDefault's "object".

    Here is a code block that I always use in my apps.

    Saving:

    -(void)savetheimage:(NSString *)imgname:(UIImage *)fromimage {

    // Now, we have to find the documents directory so we can save it
    // Note that you might want to save it elsewhere, like the cache directory,
    // or something similar.
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];

    // Now we get the full path to the file
    NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imgname];

    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(fromimage)];
    [imageData writeToFile:fullPathToFile atomically:YES];

    }


    Usage example:

     [self savetheimage:@\"myImage1.png\":imageView.image];


    To load just call this lines


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *uniquePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@\"myImage1.png\"];
    imageView.image = [[UIImage alloc] initWithContentsOfFile:uniquePath];


    It's basically getting the full path of the image (in the sandbox), and loading it up in a UIImage using initWithContentsOfFile. Pretty simple.
  • aceiswildaceiswild Posts: 62Registered Users
    rocotilos;390431 said:
    Yes, you are right. Except I think using the app sandbox directly probably is better than using NSUserDefault's "object".

    Here is a code block that I always use in my apps.

    Saving:

    -(void)savetheimage:(NSString *)imgname:(UIImage *)fromimage {

    // Now, we have to find the documents directory so we can save it
    // Note that you might want to save it elsewhere, like the cache directory,
    // or something similar.
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];

    // Now we get the full path to the file
    NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imgname];

    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(fromimage)];
    [imageData writeToFile:fullPathToFile atomically:YES];

    }


    Usage example:

     [self savetheimage:@\"myImage1.png\":imageView.image];


    To load just call this lines


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *uniquePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@\"myImage1.png\"];
    imageView.image = [[UIImage alloc] initWithContentsOfFile:uniquePath];


    It's basically getting the full path of the image (in the sandbox), and loading it up in a UIImage using initWithContentsOfFile. Pretty simple.

    Thanks for the example. Wil give it a try tonight. When I take a picture or choose from album I can easily display it in a UIImageView in the current viewController but the thing is I don't think when I want to display it in my second viewController I can't use a UIimageView because that will cause the image to be static and if it's a static image I will not be able to break the image up in puzzle pieces and move them around. It would almost have to break up the image and put each broken piece in its own UIImageView programmatically.

    But I will see what happens! Never know!

    Thanks steve
    Don't be a quitter! Never give up and keep pushing to succeed at the things you love!
  • rocotilosrocotilos Posts: 3,216iPhone Dev SDK Supporter, Registered Users
    Well, you are not supposed to be calling/messing with objects in another viewController from current viewcontroller anyways - cuz then it would violate the MVC pattern. if have to pass it around, use delegates (im not good at this yet), or as i explained, via a middle man (nsuserdefaults/sandbox).
  • aceiswildaceiswild Posts: 62Registered Users
    rocotilos;390496 said:
    Well, you are not supposed to be calling/messing with objects in another viewController from current viewcontroller anyways - cuz then it would violate the MVC pattern. if have to pass it around, use delegates (im not good at this yet), or as i explained, via a middle man (nsuserdefaults/sandbox).
    That's okay I will test with different ideas. But every bit of info I get helps !
    Thanks
    Don't be a quitter! Never give up and keep pushing to succeed at the things you love!
Sign In or Register to comment.