使用 UIImagePickerController 拍摄两次照片
Take twice photos with 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,我可以拍照而不用看相机上发生了什么。为什么它不起作用?怎么了?还有别的办法重拍吗?谢谢
你只是回忆你的UIImagePicker,它不会打开一个新的。您将不得不丢弃收到的数据。或者重新加载数据。
首先关闭之前呈现的imagePickerController。然后在完成时尝试呈现新的 imagePickerController。
在我的应用程序中,我使用 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,我可以拍照而不用看相机上发生了什么。为什么它不起作用?怎么了?还有别的办法重拍吗?谢谢
你只是回忆你的UIImagePicker,它不会打开一个新的。您将不得不丢弃收到的数据。或者重新加载数据。
首先关闭之前呈现的imagePickerController。然后在完成时尝试呈现新的 imagePickerController。