我有一个Tab栏控制器,当我点击第三个标签栏按钮时,我出现UIViewcontroller
。在这个vc的viewWillAppear
中,我提出的UIImagepickerController
工作正常。问题是当我打开视图时,我无法立即在屏幕上显示它。首先,vc出现在0.4-0.5秒后的图像选择器。所以我想首先呈现图像选择器并在用户拍摄图像后呈现vc。我也尝试从viewDidLoad
和viewWillAppear
调用选择器,但没有任何改变。
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (imagePickerWasPresented == NO)
{
imagePickerWasPresented = YES;
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = YES;
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
[self presentViewController:self.imagePicker animated:NO completion:nil];
}
}
我是在错误的地方打电话吗?
答案 0 :(得分:2)
我有同样的问题 - 而不是调用VC然后调用UIImagePicker,直接调用UIImagePicker。
完成拍照/录像时:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {}
您将转到此标准委托方法,从此处调用VC。通过这种方式,您将立即转到ImagePicker,并且只有转换才会选择对之后拍摄的内容执行某些操作,这样不那么令人沮丧/丑陋。
答案 1 :(得分:1)
不,你在一个好的地方调用它,这就是iOS的用途;如果你在彼此之上呈现多个模态,则会一个接一个地呈现,包括动画。适用于您的问题的解决方案是提供UINavigationController而不是UIViewController。将导航控制器设置为将ViewController作为根视图控制器,但也将imagepickercontroller推送到堆栈。介绍这个导航控制器,它应该直接到你的imagepickercontroller。否则,尝试将动画设置为NO的uiviewcontroller和imagepickercontroller同时显示,看看是否有效。
答案 2 :(得分:1)
尝试这一点,看看有多接近,然后适应您的需求。当我给它一个快速测试时,它似乎做了你所要求的。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ;
UIViewController * vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor whiteColor];
UINavigationController * navigationController = [[UINavigationController alloc] init];
[navigationController pushViewController:vc animated:NO];
UITabBarController * tabBarController = [[UITabBarController alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:navigationController, nil];
tabBarController.viewControllers = controllers;
tabBarController.delegate = self;
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = NO;
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
[vc presentViewController:imagePicker animated:NO completion:nil];
return YES;
}
答案 3 :(得分:1)
图库模式:
BOOL hasGallery = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = hasGalleryt ? UIImagePickerControllerSourceTypePhotoLibrary : UIImagePickerControllerSourceTypePhotoLibrary;
if (self.popoverController != nil)
{
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController=nil;
}
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:picker];
CGRect popoverRect = [self.view convertRect:[self.imageView frame]
fromView:[self.imageView superview]];
popoverRect.size.width = MIN(popoverRect.size.width, 300) ;
popoverRect.origin.x = popoverRect.origin.x;
[self.popoverController
presentPopoverFromRect:popoverRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
相机模式:
BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = hasCamera ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypePhotoLibrary;
if (self.popoverController != nil)
{
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController = nil;
}
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:picker];
CGRect popoverRect = [self.view convertRect:[self.imageView frame]
fromView:[self.imageView superview]];
popoverRect.size.width = MIN(popoverRect.size.width, 300) ;
popoverRect.origin.x = popoverRect.origin.x;
[self.popoverController
presentPopoverFromRect:popoverRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
请记得向代表们提供.h文件@interface
,如下所示:
@interface the UIViewController: UIViewController <UIPopoverControllerDelegate, UIImagePickerControllerDelegate>