Baub
Baub

Reputation: 5044

Take Photos Within Application Saving

I am trying to take photos within my application and save them to a path with two dates (to specify when the photo was taken).

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image;
    image = [info objectForKey:UIImagePickerControllerOriginalImage];
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); //save to photo album
    NSString *pathString = [NSString stringWithFormat:@"Photos/%@/%@.png",timeStarted,[NSDate date]]; //create path
    NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:pathString];
    //write the files!
    [UIImagePNGRepresentation(image) writeToFile:savePath atomically:YES];
}

When I check the folder @"Photos/%@",timeStarted it shows up as empty. What am I doing wrong?

EDIT:

Here is my new code for testing purposes:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //Create the image and save it to the photo album
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

    //Create the directory and add the file name to it
    NSString *fileName = @"Test.png";
    NSString *directory = [NSHomeDirectory() stringByAppendingPathComponent:@"Photos"];
    NSString *fullPath = [directory stringByAppendingPathComponent:fileName];
    NSLog(@"Full path: %@", fullPath);

    //Save "image" to the file path
    [UIImagePNGRepresentation(image) writeToFile:fullPath atomically:YES];

    //START CHECKING : Log to check if anything was saved
    NSError *error;
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSLog(@"Photos directory: %@", [fileMgr contentsOfDirectoryAtPath:directory error:&error]);
    //END CHECKING

    [camera dismissModalViewControllerAnimated:YES];
}

And my NSLog reads:

[640:707] Full path: /var/mobile/Applications/3EEDBD68-5496-458D-9EF0-062746847C83/Photos/Test.png
[640:707] Photos directory: (null)

Upvotes: 1

Views: 455

Answers (1)

DHamrick
DHamrick

Reputation: 8488

Your pathString contains an invalid path. If you NSLog your path string it will look something like this

Photos/2011-11-16 20:16:16 +0000/2011-11-16 20:16:16 +0000.png

Try saving it using a regular path such as Photo.png. If this works it will indicate that it is a problem with the path. After this try using NSDateFormatter to output the dates as a string that is a valid path.

Upvotes: 2

Related Questions