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.

saving a photo to an NSMutableDictionary in iPhone app

gbennagbenna Posts: 99Registered Users
I have an app which has a MainViewController (MVC) and a DetailViewController (DVC).

From the MVC I use an NSMutableDictionary to load an image view and a text view in the DVC depending on which image in the MVC is tapped (kind of like a tableview. I also have an empty UIImageView in the DVC.

Inside the DVC I have a button to open the camera. i can take a photo and save it to the app's documents folder and it shows up in the empty UIImageView. However when I return to the MVC and go back to the same called DVC where the photo was displayed it is no longer displayed even though I know it has been saved into the documents. If I tap on a different image in the MVC and open a different DVC grouping I can take a photo but the problem is I don't know how to progressively name the saved images.

What I would like to do is have the photo taken in DVC #1 be saved as image #1 and show up every time I open DVC #1 and then when I open DVC #1 and take a photo it is saved as image #2 and stays displayed in it and so forth and so on. First off is this possible? And admitting that I am rather new to coding and I think here way over my head could someone help me accomplish this.

Here is my code for the MVC :


- (void) coverflowView:(TKCoverflowView*)coverflowView coverAtIndexWasDoubleTapped:(int)index{

SomeDetailViewController *detailViewController = [[SomeDetailViewController alloc] initWithNibName:@"SomeDetailViewController" bundle:nil];

if ((int)index == 0) {
NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"0002.jpg", @"imagekey",
@"This is #1", @"textkey",


NSLog(@"%@", [myDictionary objectForKey:@"imagekey"]);
NSLog(@"%@", [myDictionary objectForKey:@"textkey"]);
detailViewController.myDictionary = myDictionary;
}

else if ((int)index == 1) {
NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"0003.jpg", @"imagekey",
@"This is #2", @"textkey",

NSLog(@"%@", [myDictionary objectForKey:@"imagekey"]);
NSLog(@"%@", [myDictionary objectForKey:@"textkey"]);
detailViewController.myDictionary = myDictionary;
}



[self.navigationController pushViewController:detailViewController animated:YES];


}

and here is my DVC:


- (void)viewDidLoad {

[super viewDidLoad];
imageView .image = [[UIImage alloc] init];
self.imageView.image =[UIImage imageNamed:[(NSMutableDictionary *)myDictionary objectForKey:@"imagekey"]];

self.myText.text =[(NSMutableDictionary *)myDictionary objectForKey:@"textkey"];

NSLog(@"%@", [myDictionary objectForKey:@"imagekey"]);
NSLog(@"%@", [myDictionary objectForKey:@"textkey"]);

}


-(IBAction)getPhoto:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera ;
[self presentModalViewController: picker animated:YES];

}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
{
[picker dismissModalViewControllerAnimated:YES];
}

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

//this is where I declare the name and directory for the photo

NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"];
// Access the uncropped image from info dictionary

NSError *error;
//try this to documents
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];


NSFileManager *fileMgr = [NSFileManager defaultManager];

NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);


UIAlertView *alert;

// Unable to save the image
if (error)
alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Unable to save image to Documents."
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
else // All is well
alert = [[UIAlertView alloc] initWithTitle:@"Success"
message:@"Image saved to Documents."
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil];

[alert show];

//this is the view where the photo is shown in the DVC

photoView.image = image;

[self dismissModalViewControllerAnimated:YES];

}
Post edited by gbenna on

Replies

  • teCmateCma Posts: 255Registered Users
    gbenna;410836 said:
    I have an app which has a MainViewController (MVC) and a DetailViewController (DVC).

    From the MVC I use an NSMutableDictionary to load an image view and a text view in the DVC depending on which image in the MVC is tapped (kind of like a tableview. I also have an empty UIImageView in the DVC.

    Inside the DVC I have a button to open the camera. i can take a photo and save it to the app's documents folder and it shows up in the empty UIImageView. However when I return to the MVC and go back to the same called DVC where the photo was displayed it is no longer displayed even though I know it has been saved into the documents. If I tap on a different image in the MVC and open a different DVC grouping I can take a photo but the problem is I don't know how to progressively name the saved images.

    What I would like to do is have the photo taken in DVC #1 be saved as image #1 and show up every time I open DVC #1 and then when I open DVC #1 and take a photo it is saved as image #2 and stays displayed in it and so forth and so on. First off is this possible? And admitting that I am rather new to coding and I think here way over my head could someone help me accomplish this.

    Here is my code for the MVC :


    - (void) coverflowView:(TKCoverflowView*)coverflowView coverAtIndexWasDoubleTapped:(int)index{

    SomeDetailViewController *detailViewController = [[SomeDetailViewController alloc] initWithNibName:@\"SomeDetailViewController\" bundle:nil];

    if ((int)index == 0) {
    NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
    @\"0002.jpg\", @\"imagekey\",
    @\"This is #1\", @\"textkey\",


    NSLog(@\"%@\", [myDictionary objectForKey:@\"imagekey\"]);
    NSLog(@\"%@\", [myDictionary objectForKey:@\"textkey\"]);
    detailViewController.myDictionary = myDictionary;
    }

    else if ((int)index == 1) {
    NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
    @\"0003.jpg\", @\"imagekey\",
    @\"This is #2\", @\"textkey\",

    NSLog(@\"%@\", [myDictionary objectForKey:@\"imagekey\"]);
    NSLog(@\"%@\", [myDictionary objectForKey:@\"textkey\"]);
    detailViewController.myDictionary = myDictionary;
    }



    [self.navigationController pushViewController:detailViewController animated:YES];


    }

    and here is my DVC:


    - (void)viewDidLoad {

    [super viewDidLoad];
    imageView .image = [[UIImage alloc] init];
    self.imageView.image =[UIImage imageNamed:[(NSMutableDictionary *)myDictionary objectForKey:@\"imagekey\"]];

    self.myText.text =[(NSMutableDictionary *)myDictionary objectForKey:@\"textkey\"];

    NSLog(@\"%@\", [myDictionary objectForKey:@\"imagekey\"]);
    NSLog(@\"%@\", [myDictionary objectForKey:@\"textkey\"]);

    }


    -(IBAction)getPhoto:(id)sender
    {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera ;
    [self presentModalViewController: picker animated:YES];

    }

    -(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
    {
    [picker dismissModalViewControllerAnimated:YES];
    }

    - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
    UIImage *image = [info objectForKey:@\"UIImagePickerControllerOriginalImage\"];

    //this is where I declare the name and directory for the photo

    NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@\"Documents/Test.png\"];
    // Access the uncropped image from info dictionary

    NSError *error;
    //try this to documents
    [UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];


    NSFileManager *fileMgr = [NSFileManager defaultManager];

    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@\"Documents\"];

    NSLog(@\"Documents directory: %@\", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);


    UIAlertView *alert;

    // Unable to save the image
    if (error)
    alert = [[UIAlertView alloc] initWithTitle:@\"Error\"
    message:@\"Unable to save image to Documents.\"
    delegate:self cancelButtonTitle:@\"Ok\"
    otherButtonTitles:nil];
    else // All is well
    alert = [[UIAlertView alloc] initWithTitle:@\"Success\"
    message:@\"Image saved to Documents.\"
    delegate:self cancelButtonTitle:@\"Ok\"
    otherButtonTitles:nil];

    [alert show];

    //this is the view where the photo is shown in the DVC

    photoView.image = image;

    [self dismissModalViewControllerAnimated:YES];

    }
    Use code tags - please.
  • gbennagbenna Posts: 99Registered Users
    teCma;410975 said:
    Use code tags - please.
    First off I tried saving as a jpg but it didn't matter.

    What I have done is create a variable in my NSDictionary which is the path for the saved photo @"Documents/photoname.jpg". This insures that the name of the photo in each different incarnation of the DVC will save a photo that is specific to that DVC. I then created another UIImageView in the DVC which I then load from the MVC's NSDictionary using the name of the photo I took from that DVC. I know that the photo is being saved into the Documents directory with the desired name because when I use NSLog it shows that that photo name is in the directory. And I know it is being loaded from the MVC into the DVC because of NSLog on both view controllers. However The view doesn't show up in the specified UIImage View. I know that the UIImageView is connected up correctly because if I call another key for the object it works.
    The only thing I can think is that I am not calling the saved photo correctly from the Documents Directory into the NSMutableDictionary or the photo is not being saved as an image. Is there a way I can check this?

    Since most of the images I am calling in the NSMutableDictionary are from the main bundle of the app do I need to do something to put things from the Documents directory into the main bundle or is there a different way to call images saved in the Documents Directory.

    Here is my added code.

    this is put into my MVC's NSMutableDicationary

    @"camera image 1.jpg", @"photoImage",
    @"Documents/camera image 1.jpg", @"photokey",nil];

    This was added to the DVC's ViewDidLoad

    photoView2 .image = [[UIImage alloc] init];
    self.photoView2.image =[UIImage imageNamed:[(NSMutableDictionary *)myDictionary objectForKey:@"photoImage"]];

    and this I put in my DVC's image pickers code

    NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:[(NSMutableDictionary *)myDictionary objectForKey:@"photokey"]];

    and ideas are very much appreciated.

    One more thing when I take the picture and save it it does show up as a preview and it does day it is saved in the documents directory.
  • teCmateCma Posts: 255Registered Users
    gbenna;411182 said:
    First off I tried saving as a jpg but it didn't matter.

    What I have done is create a variable in my NSDictionary which is the path for the saved photo @"Documents/photoname.jpg". This insures that the name of the photo in each different incarnation of the DVC will save a photo that is specific to that DVC. I then created another UIImageView in the DVC which I then load from the MVC's NSDictionary using the name of the photo I took from that DVC. I know that the photo is being saved into the Documents directory with the desired name because when I use NSLog it shows that that photo name is in the directory. And I know it is being loaded from the MVC into the DVC because of NSLog on both view controllers. However The view doesn't show up in the specified UIImage View. I know that the UIImageView is connected up correctly because if I call another key for the object it works.
    The only thing I can think is that I am not calling the saved photo correctly from the Documents Directory into the NSMutableDictionary or the photo is not being saved as an image. Is there a way I can check this?

    Since most of the images I am calling in the NSMutableDictionary are from the main bundle of the app do I need to do something to put things from the Documents directory into the main bundle or is there a different way to call images saved in the Documents Directory.

    Here is my added code.

    this is put into my MVC's NSMutableDicationary

    @"camera image 1.jpg", @"photoImage",
    @"Documents/camera image 1.jpg", @"photokey",nil];

    This was added to the DVC's ViewDidLoad

    photoView2 .image = [[UIImage alloc] init];
    self.photoView2.image =[UIImage imageNamed:[(NSMutableDictionary *)myDictionary objectForKey:@"photoImage"]];

    and this I put in my DVC's image pickers code

    NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:[(NSMutableDictionary *)myDictionary objectForKey:@"photokey"]];

    and ideas are very much appreciated.

    One more thing when I take the picture and save it it does show up as a preview and it does day it is saved in the documents directory.
    Dude. I didn't mean comments. When you are typing code in this forum use the available code tags to make the code easier to read.
Sign In or Register to comment.