5

Hey I'm new to iPhone and I have been trying to make an app recently. Basically, what I want to do is if user will capture any image from camera, then it should save in the device gallery. I know how to save the photo in gallery, it's working for me, but I am having trouble with to save all the captured images into a specific folder (like a new album) in device gallery. I want the user to be able to take several pictures and then copy pictures from that specific folder to the gallery.

here is my code:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];

    if([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
        UIImage *photoTaken = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

        self.imageView.image = photoTaken;

        //Save Photo to library only if it wasnt already saved i.e. its just been taken
        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
            UIImageWriteToSavedPhotosAlbum(photoTaken, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        }
    }

    [picker dismissViewControllerAnimated:YES completion:NULL];
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    UIAlertView *alert;
    if (error) {
        alert = [[UIAlertView alloc] initWithTitle:@"Error!"
                                           message:[error localizedDescription]
                                          delegate:nil
                                 cancelButtonTitle:@"OK"
                                 otherButtonTitles:nil];
        [alert show];
    }
}

I have tried by doing this below code, but i don't know where it is saving images to the App resource in my iPhone device:

    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"EMS_FOLDER"];

    NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"Files"];

Any source or link for reference is appreciated. Thanks for the help!

user2786
  • 656
  • 4
  • 13
  • 35
  • possible duplicate of [Save Photos to Custom Album in iPhones Photo Library](http://stackoverflow.com/questions/10954380/save-photos-to-custom-album-in-iphones-photo-library) – Desdenova Mar 27 '14 at 07:40
  • @DesdenovaThanks it is saving in a different folder, can we create sub folder inside the main album folder? – user2786 Mar 27 '14 at 07:47
  • I don't understand what is the main album folder but you can create as much album as you like. – Desdenova Mar 27 '14 at 07:55
  • @DesdenovaSorry, i wanted to ask that suppose i have created one album folder in iPhone gallery with name "My App Images", so i want to know that Is it possible to create sub folder inside the "My App Images" album. So user can differentiate all images easily. – user2786 Mar 27 '14 at 08:02
  • No sorry, no sub albums. – Desdenova Mar 27 '14 at 08:09
  • Okey..Thanks alot @Desdenova for giving me time. Can we rename the photo name according to our wish? – user2786 Mar 27 '14 at 08:13

1 Answers1

3

You can use AssetsLibrary, this will solve your problem.

- (IBAction)takePhoto
{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePickerController.editing = YES;
    imagePickerController.delegate = (id)self;

    [self presentModalViewController:imagePickerController animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [self.library saveImage:image toAlbum:@"Touch Code Magazine" withCompletionBlock:^(NSError *error) {
        if (error!=nil) {
            NSLog(@"Big error: %@", [error description]);
        }
    }];

    [picker dismissModalViewControllerAnimated:NO];
}

This has been explained here, a nice tutorial..

http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/

Kundan
  • 3,084
  • 2
  • 28
  • 65
  • @StarkThanks, As discussed with Desdenova, its saving all the images in the specific folder with name "Touch Code Magazine" but i want know that Is it possible to create sub folders inside main folder "Touch Code Magazine" in gallery. – user2786 Mar 27 '14 at 07:50