基本上,我希望应用程序立即打开相机,就像Snapchat一样。所以AppDelegate会指向CameraViewController。它打开很好,我可以拍照,它将重定向到该图片的“ConfirmationViewController”。但是,如果我从库中进行选择,它会崩溃,因为“ConfirmationViewController”正在从“CameraViewController”调用,而“CameraViewController”不再位于视图层次结构中,因为它调用了“LibraryImagePicker”。
这是一张图
## AppDelegate ##
|
## CameraViewController ##
| |
(photo captured) (chose from libary button selected)
| |
## ConfirmImageViewController ## ## LibraryPickerViewController ##
|
(selected image)
|
## ConfirmImageViewController ##
我的代码如下:
CameraViewController:
- (void)viewDidAppear:(BOOL)animated
{
[self setNeedsStatusBarAppearanceUpdate];
UIView *backgroundScreen = self.view;
backgroundScreen.backgroundColor = [UIColor colorWith8BitRed:32 green:32 blue:32 alpha:1];
self.imagePickerController = [[UIImagePickerController alloc] init];
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePickerController.view.autoresizingMask = NO;
self.imagePickerController.showsCameraControls = NO;
self.imagePickerController.delegate = self;
self.imagePickerController.cameraViewTransform = CGAffineTransformMakeScale(scale, scale);
CGRect f = self.view.window.bounds;
UIView *overlayView = [[UIView alloc] initWithFrame:f];
self.imagePickerController.cameraOverlayView = overlayView;
//cosmetic stuff, adding buttons to overlay view
[self presentViewController:self.imagePickerController animated:NO completion:nil];
When a photo is captured by pressing the capture button, I call a function that sets up a "ConfirmImageViewController" and then has self.imagePickerController present that view controller.
[self.imagePickerController presentViewController:self.confirmImageViewController animated:NO completion:nil];
That works fine. But if I select a photo from the library, it crashes because once the photo is selected in calls the same function above, which has
[self.imagePickerController presentViewController:self.confirmImageViewController animated:NO completion:nil];
Here's the code for when I select to choose from my photo library:
- (void)chooseFromLibraryButtonClicked: (id)sender;
{
UIImagePickerController *libraryPicker = [[UIImagePickerController alloc] init];
[libraryPicker.view setFrame:CGRectMake(0, self.view.bounds.size.height/2, self.view.bounds.size.width, self.view.bounds.size.height/2)];
[libraryPicker setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
[libraryPicker setDelegate:self];
[self.imagePickerController presentViewController:libraryPicker animated:YES completion:nil];
}
当按下捕获按钮捕获照片时,我调用一个设置“ConfirmImageViewController”的函数,然后让self.imagePickerController显示该视图控制器。
imagePickerController presentViewController:self.confirmImageViewController animated:NO completion:nil];
工作正常。但是,如果我从库中选择一张照片,它会崩溃,因为一旦在调用中选择了相同的功能,就会有上面的相同功能
[self.imagePickerController presentViewController:self.confirmImageViewController animated:NO completion:nil];
以下是我选择从照片库中选择时的代码:
- (void)chooseFromLibraryButtonClicked: (id)sender;
{
UIImagePickerController *libraryPicker = [[UIImagePickerController alloc] init];
[libraryPicker.view setFrame:CGRectMake(0, self.view.bounds.size.height/2, self.view.bounds.size.width, self.view.bounds.size.height/2)];
[libraryPicker setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
[libraryPicker setDelegate:self];
[self.imagePickerController presentViewController:libraryPicker animated:YES completion:nil];
}