How to save images from Camera to specific folder

2019-06-21 19:53发布

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!

1条回答
孤傲高冷的网名
2楼-- · 2019-06-21 20:35

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/

查看更多
登录 后发表回答