无法弹出 UIImagePickerController
Posted
技术标签:
【中文标题】无法弹出 UIImagePickerController【英文标题】:Can't pop UIImagePickerController 【发布时间】:2015-05-29 12:55:52 【问题描述】:我有一个带有自定义叠加层的UIImagePickerController
。我正在尝试实现一个取消按钮,它只会弹出UIImagePickerController
。我的设置代码如下:
- (void)tapPhoto
//setup the picker
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.cameraDevice=UIImagePickerControllerCameraDeviceFront;
self.picker.showsCameraControls=NO;
self.cameraOverlay = [[CameraOverlay alloc] init];
UIImage *camoverlayImage = self.cameraOverlay.cameraOverlayImage;
UIImageView *camoverlayImageView = [[UIImageView alloc] initWithImage:camoverlayImage];
camoverlayImageView.frame=CGRectMake(0, 0, self.cameraOverlay.previewSize.width , self.cameraOverlay.previewSize.height);
//init the control view
UIView *cameraControls= [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
[cameraControls addSubview:camoverlayImageView];
//Shutter Button
UIButton *button = [[UIButton alloc] init];//70*70
[button setBackgroundImage:[UIImage imageNamed:@"cameraoff"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"cameraon"] forState:UIControlStateHighlighted];
button.frame = CGRectMake(([[UIScreen mainScreen] bounds].size.width/2)-70/2, [[UIScreen mainScreen] bounds].size.height-70-10, 70 , 70);
[button addTarget:self action:@selector(takePhoto) forControlEvents:UIControlEventTouchUpInside];
[cameraControls addSubview:button];
UIButton *button2 = [[UIButton alloc] init];
[button2 setTitle:@"Cancel" forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(cancelPhoto) forControlEvents:UIControlEventTouchUpInside];
button2.frame = CGRectMake(5, [[UIScreen mainScreen]bounds].size.height-30, 0, 0);
[button2 sizeToFit];
[cameraControls addSubview:button2];
[self.picker setCameraOverlayView:cameraControls];
[self presentViewController:self.picker animated:YES completion:NULL];
-(void)cancelPhoto
NSLog(@"photo canceled");
[self.navigationController pushViewController:self.picker animated:YES];
-(void)takePhoto
//this will auto trigger the control
[self.picker takePicture];
我试图从导航控制器中弹出它,但没有任何运气。我也试过[self.picker popViewControllerAnimated:YES];
。谁能给我任何关于为什么这不起作用的指示?谢谢
【问题讨论】:
【参考方案1】:您正在(正确地)展示您的选择器:
[self presentViewController:self.picker animated:YES completion:NULL];
present
的反义词不是push或pop,而是dismiss
:
[self dismissViewControllerAnimated:YES completion:nil];
【讨论】:
【参考方案2】:首先这一行是错误的!
[self.navigationController pushViewController:self.picker animated:YES];
当你的选择器已经出现在你当前的视图控制器中时,你为什么要推送选择器
正确的方法是这样的:-
-(void)cancelPhoto
//as you have presented the view controller so have dismiss it by this way.
[self.picker dismissViewControllerAnimated:YES completion:nil];
【讨论】:
【参考方案3】:试试这样-
-(void)tapPhoto
//set up the Picker
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.cameraDevice=UIImagePickerControllerCameraDeviceFront;
//add a UIView as CameraOverLay
UIView *overlayView = [[UIView alloc] initWithFrame:picker.view.frame];
[self.picker setCameraOverlayView:overlayView];
//Present the Picker
[self presentViewController:picker animated:YES completion:nil];
这样,您应该默认有一个取消按钮。然后只需实现委托方法并在您点击取消按钮时关闭控制器。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
//get the captured image if any
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)
== kCFCompareEqualTo)
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//save the image in photo library
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
//pop out or remove the picker
[picker dismissViewControllerAnimated:YES completion:nil];
【讨论】:
【参考方案4】:不要尝试使用popViewControllerAnimated
。而是尝试:
[self.picker dismissViewControllerAnimated:YES completion:nil];
【讨论】:
以上是关于无法弹出 UIImagePickerController的主要内容,如果未能解决你的问题,请参考以下文章