gpt4 book ai didi

ios - 使用 UIImagePickerController 拍摄两次照片

转载 作者:行者123 更新时间:2023-11-28 21:48:05 26 4
gpt4 key购买 nike

在我的应用程序中,我使用 UIImagePickerController 拍照,当拍照时,应用程序会显示另一个 View Controller ,我在其中为用户提供了在他的照片中添加农场的可能性。在这个 View Controller 中,我将给用户重拍照片的机会,所以我创建了一个按钮来调用 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,我可以在看不到相机上发生了什么的情况下拍照。为什么它不起作用?怎么了?还有别的办法重拍吗?谢谢

最佳答案

首先关闭之前呈现的 imagePickerController。然后在完成时尝试呈现新的 imagePickerController。

关于ios - 使用 UIImagePickerController 拍摄两次照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29348070/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com