如何将相机拍摄的照片保存到 iPhone 相册中的特定文件夹?

5

嘿,我是 iPhone 的新手,最近一直在尝试制作应用程序。基本上,我的想法是如果用户从相机中拍摄了任何图像,则应该将其保存在设备图库中。我知道如何将照片保存在图库中,这对我来说没问题,但我在将所有捕获的图像保存到设备图库中的特定文件夹(例如一个新相册)方面遇到了麻烦。我希望用户能够拍摄多张照片,然后将那个特定文件夹中的图片复制到图库中。

以下是我的代码:

- (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设备的应用程序资源的位置:
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

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

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

任何参考来源或链接都会受到赞赏。 感谢您的帮助!

可能是在iPhone照片库中保存照片到自定义相册的重复问题。 - Desdenova
@Desdenova谢谢,它保存在不同的文件夹中,我们可以在主相册文件夹内创建子文件夹吗? - user2786
我不明白什么是主要相册文件夹,但您可以创建尽可能多的相册。 - Desdenova
@Desdenova 抱歉,我想问一下,假设我在 iPhone 相册中创建了一个名为“我的应用图片”的相册文件夹,那么我想知道是否可以在“我的应用图片”相册内创建子文件夹,以便用户可以轻松区分所有图片。 - user2786
没有抱歉,也没有子相册。 - Desdenova
好的,非常感谢@Desdenova给我时间。我们可以根据自己的意愿重新命名照片名称吗? - user2786
1个回答

3
您可以使用AssetsLibrary,这将解决您的问题。
- (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];
}

这里有一篇很好的教程,讲解了如何在iOS5中创建自定义相册并保存照片。你可以查看链接:http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/

@Stark感谢,如与Desdenova讨论的那样,它正在使用名称“Touch Code Magazine”将所有图像保存在特定文件夹中,但我想知道在相册中是否可以创建主文件夹“Touch Code Magazine”的子文件夹。 - user2786

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接