如何将图像从相机保存到iPhone库中的特定文件夹?

时间:2014-03-27 07:37:50

标签: ios iphone camera uiimagepickercontroller

嘿,我是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设备中将图像保存到App资源的位置:

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

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

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

赞赏任何参考资料或链接。 谢谢你的帮助!

1 个答案:

答案 0 :(得分: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];
}

这里已经解释了这个很好的教程..

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