如何将相机中的图像保存到iPhone图库中的特定文件夹? [英] How to save images from Camera to specific folder in iPhone gallery?

查看:502
本文介绍了如何将相机中的图像保存到iPhone图库中的特定文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我刚接触iPhone,最近一直在尝试制作一个应用。基本上,我想做的是,如果用户将从相机中捕获任何图像,则应将其保存在设备库中。我知道如何将照片保存在图库中,这对我来说很有效,但是我无法将所有捕获的图像保存到设备图库中的特定文件夹(例如新相册)中。我希望用户能够拍摄多张图片,然后将图片从该特定文件夹复制到图库。

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.

这是我的代码:

- (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];
    }
}

我已经尝试通过以下代码执行此操作,但是我不知道它将图像保存到我的iPhone设备中的App资源的位置:

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!

推荐答案

您可以使用AssetsLibrary,这将解决您的问题。

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/

这篇关于如何将相机中的图像保存到iPhone图库中的特定文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆