Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

gbenna

macrumors member
Original poster
Jul 27, 2011
62
0
I'm not sure if this is possible, but I hope it is and I need some help doing it if so.

I have a mainView in which I have an NSMutableDictionary.
In that Dictionary is a key (identity) which gives the name for the text entered in one textField.
Another key (number key) gives the name for the text entered in a second textField
After these two fields are entered a button bar is clicked and the keyboard disappears and the .txt files are stored in the Documents Directory.

another key (photoKey) in the Dictionary sets the path and gives the name for a photo the user takes with the camera.

What I wish to do is have the text entered into the second textfield be the name for the photo given in the key (photoKey).

So lets say the user enters the number 1 into the (numberkey) textfield. I then want the name of the photo in the (photoKey) to be 1.jpg.

Is this possible?

Here is my code;

MainView handing off the NSMutableDictionary;


Code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    const NSDictionary *const row = [self rowForIndexPath:indexPath];
    
    NSString *wantedClassName = [row objectForKey:@"controller"];
    
    UIViewController *const vc = [[NSClassFromString (wantedClassName) alloc] init];
    NSLog(@"controller is -%@-", wantedClassName);
    
    
    if ([vc isKindOfClass:[DetailViewController class]])
    {
       DetailViewController*lcVC =(DetailViewController*)vc;
        
               
        NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   //I have tried this but it isn't working, it only names the photo (image name.txt).jpg 

                                                   @"Documents/(imagename.txt).jpg",@"photokey",
                                                 @"name.txt", @"identity", 
                                                 @"imagename.txt",@"numberkey",nil];
        lcVC.myDictionary = myDictionary;   
        
        NSLog(@"photokey");//I know this isn't correct, how would I get the exact name of the photo from this?
         }

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

In my DetailViewController I use this to take the picture;

Code:
-(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"];
    
    NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:[(NSMutableDictionary *)myDictionary objectForKey:@"photokey"]];
    // Access the uncropped image from info dictionary
    
    NSError *error;
  
    [UIImageJPEGRepresentation(image, 0.5) writeToFile:jpgPath atomically:YES]; 
    
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    
    NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
     }


Any Ideas will be greatly appreciated!
 
Last edited:

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
You're just storing the name of the image file as an NSString, right? So, you should look into 'formatting a string' (that's a big hint) to hold what you need.
 

gbenna

macrumors member
Original poster
Jul 27, 2011
62
0
naming photo taken in app using a textfield

Dejo,
Thanks for the hint. Is this to retrieve the information in NSLog or inside the NSMutableDictionary
@"a.jpg", @photoKey", ?

sorry to hear about your riding partner(bicycle riding?) What kind of cancer? How old is he/she?
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Dejo,
Thanks for the hint. Is this to retrieve the information in NSLog or inside the NSMutableDictionary
@"a.jpg", @photoKey", ?
I'm not entirely sure what your question is, but it has nothing to do with retrieving a string. It has to do with building a string to contain your desired filename (@"1.jpg", etc.), which is what you wanted, right?

sorry to hear about your riding partner(bicycle riding?) What kind of cancer? How old is he/she?
Yes, bike riding. And he doesn't have cancer. But he is getting his head shaved anyways as a sign of support for the children who do have cancer.
 

gbenna

macrumors member
Original poster
Jul 27, 2011
62
0
naming photo taken in app using a textfield

So this is the other part of the equation:

I want to take that photo and put it into a NSMutableArray;

So this is the code for that:


I have the user take a photo inside an app and store it inside the Document's Directory.

I want to grab the Photo and use it in an NSMutableArray.

The photo is saved ok and I can load it into a UIImageView.

Here is my code and what I am attempting to do;

Code:
- (void)viewDidLoad
{

   NSString *imageName = [myDictionary objectForKey:@"photoimagekey"];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    //Get a full path to the image in the documents directory.
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:imageName];

    [super viewDidLoad];

    //How do I get the imageName from the fullPath to the directory??
    //I want the image to have the name entered into the textfield so that the first photo that is saved will be the first image in the image array the second image saved will be second etc.

    Pictures = [NSMutableArray arrayWithObjects:

              [UIImage imageNamed:@"?.jpg"],//this is where I want to put the image from the Document's directory.  I know the name of the photo but it can't seem to use it.
              [UIImage imageNamed:@"0002.jpg"],
              [UIImage imageNamed:@"0003.jpg"],
              [UIImage imageNamed:@"0004.jpg"],
              [UIImage imageNamed:@"0005.jpg"],
              [UIImage imageNamed:@"0006.jpg"],
              [UIImage imageNamed:@"0007.jpg"],
              [UIImage imageNamed:@"0008.jpg"],
              [UIImage imageNamed:@"0009.jpg"],nil];

}
 
Last edited by a moderator:

gbenna

macrumors member
Original poster
Jul 27, 2011
62
0
naming photo taken in app using a textfield

No I am not done with the first part of the question. You wanted clarification as to what I wanted from the first question. I want to take a picture and have it named by the name that is put into the TextField Like '1' then that will be used to be put into the NSArray in the second question (which I wasn't trying to get an answer for yet) Also I want to name the photo what is put into the textfield so I can retrieve it at a specific place using a sequence of # like 1.jpg, 2.jpg etc. Thanks
 

gbenna

macrumors member
Original poster
Jul 27, 2011
62
0
naming photo taken in app using a textfield

So I figured out that I can name a string using the UITextField like this

Code:
NSString *photokey2 = [[self imageNameTextField]text];

And I am trying to use this in here

Code:
NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:photokey2];

inside my UIImagePickerController but to no good.

But I think I am getting closer;
any suggestions

I think I will have to format the uitextfield # like 1 so it is 1.jpg though
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
I think I will have to format the uitextfield # like 1 so it is 1.jpg though

Or, more specifically, an NSString containing @"1.jpg". So, baby steps! Deal with this issue first before moving on to the next part of the problem. Divide-and-conquer. You want to create a string that contains @"n.jpg", where n is the "number" the user typed into the textField. Well, you already know how to get that number from the textField (as a string, even). But you need to learn how to create a string that contains that number-as-a-string followed by ".jpg". This process is normally known as formatting a string object. Read the String Programming Guide, particularly the section on Formatting String Objects. Come back here when you think you have a solution to this very small piece of the entire puzzle and let us know what you propose.
 

gbenna

macrumors member
Original poster
Jul 27, 2011
62
0
naming photo taken in app using a textfield

Ok, So I think I have appended the sting's format like this.

If I want to change the UITextField text that has been enter so that the text in the text file is 1.jpg then I do this


Code:
 NSArray *documentPaths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory2 = [documentPaths2 objectAtIndex:0];
    NSString *documentTXTPath2 = [documentsDirectory2 stringByAppendingPathComponent:[(NSMutableDictionary *)familyDictionary objectForKey:@"numberkey"]];

//this says I am going to save the text entered into the textfield
    NSString *savedString2 = imageNameTextField.text;

//this says I an going to append that string so that I will add .jpg to the end of it  so now it is 1.jpg  MyNSLog shows this as well as when I look at my documents file in the iPhone simulator.
    savedString2 = [savedString2 stringByAppendingFormat:@".jpg"];   
    NSLog(@"The Saved File is:%@", savedString2);
    [savedString2 writeToFile:documentTXTPath2 atomically:YES
                    encoding:NSUTF8StringEncoding error:nil];


If I want to just save the name of the photo as 1.jpg I would do it inside the the imagepickercontroller like this


Code:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    picker.allowsEditing = YES;
     NSString *photokey2 = [[self imageNameTextField]text];
//this is where I append the string
  photokey2 = [photokey2 stringByAppendingFormat:@".jpg"];
//still I know this is wrong
    NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:photokey2];    // Access the uncropped image from info dictionary

    [UIImageJPEGRepresentation(image, 0.5) writeToFile:pngPath atomically:YES]; 
    
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    
    NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);

I don't know if either way is better but I thing to append the text file before it is saved would be better. What do you think
 

gbenna

macrumors member
Original poster
Jul 27, 2011
62
0
naming photo taken in app using a textfield

I have been doing some reading and thinking and I think I should append the textfield with .jpg because it really serves no other purpose but to name the photo taken. Then i was working on getting the name attached to the photo and I have tried several things but in my reading. I found out that when I used the code with (homeDirectory) in it. that was for use with OSX basically and I should be using the same code that I use with the textfields when I save the photo so I have change that code to;

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


- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    picker.allowsEditing = YES;
// I tried this to name the image no go
  image =UIImage imageNamed: [[self imageNameTextField]text];
 
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
//and I tried this to append the path but to no avail.
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[[self imageNameTextField]text]]; 
    NSError *error;
    
    
    [UIImageJPEGRepresentation(image, 0.5) writeToFile:fullPath atomically:YES];


I am a retired art teacher who teaches ceramics in a retirement community on tuesday mornings so won't get back to this until this afternoon.

Oh, one more thing I was thinking that when I search the directory I should not search for each file separately, but for all jpg files and then put them into an array numerically. I'm sure this is possible just need to figure out how.

Thanks for your help
 

gbenna

macrumors member
Original poster
Jul 27, 2011
62
0
naming photo taken in app using a textfield

So, I was wrong, It is better to append the photo name with .jpg. Now that I have that working I know it but it took some tries and lots o errors.

My code.


Code:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    picker.allowsEditing = YES;
   NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[[self imageNameTextField]text]];
   
 
    
    fullPath = [fullPath stringByAppendingFormat:@".jpg"];
    NSError *error;
    //try this to documents
    
    [UIImageJPEGRepresentation(image, 0.5) writeToFile:fullPath atomically:YES]; 
    
 }

Now to figure out how to put it into the array or better yet how I can find all .jpgs in the documents directory and put them in the array numerically.

thanks for your help
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.