使用UIImagePickerController拍摄两张照片

时间:2015-03-30 13:55:56

标签: ios objective-c uiimagepickercontroller

在我的应用程序中,我通过使用UIImagePickerController拍照,当拍摄照片时,应用程序显示另一个视图控制器,我在其中向用户提供将农场添加到他的照片的可能性。在这个视图控制器中,我将给用户重新拍摄图片的机会,所以我创建了一个按钮来调用UIImagePickerController,我使用了以下代码:

- (IBAction)buttonRetakePic:(UIBarButtonItem *)sender {
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
    [self presentViewController:picker animated:YES completion:nil];
}

#pragma mark imagePicker delegate
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSError *error;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"/Immagini"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:imagePath]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:imagePath withIntermediateDirectories:NO attributes:nil error:&error];
    }

    NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:imagePath error:nil];
    long count = [fileList count];
    NSString *filename = [NSString stringWithFormat:@"Nivea_%ld.jpg", count++];
    NSString *imagePathWithFileName = [imagePath stringByAppendingPathComponent:filename];
    [[NSUserDefaults standardUserDefaults]setObject:filename forKey:@"Filename"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    UIImage *picture = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    NSData *thumbData = UIImageJPEGRepresentation(picture, 1);
    [thumbData writeToFile:imagePathWithFileName atomically:YES];

    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"Picture"];
    [[[[UIApplication sharedApplication] delegate] window] setRootViewController:vc];
}

但是当我按下buttonBack时,它会向我显示带有旧图片的UIImagePickerController,我可以拍照而不会看到相机上发生了什么。它为什么不起作用?怎么了?还有另一种重拍方式吗?谢谢

2 个答案:

答案 0 :(得分:1)

您只是回想起您的UIImagePicker,它不会打开一个新的。您将不得不丢弃您收到的数据。或者重新加载数据。

答案 1 :(得分:1)

首先关闭之前提供的imagePickerController。然后在完成时尝试展示新的imagePickerController。