使用UIImagePickerController完成后加载nib文件

时间:2011-07-13 04:06:36

标签: iphone ios uiimagepickercontroller

我想知道在使用UIImagePicker Controller完成后如何加载新的nib文件。因此,在用户从相机或相册拍摄照片后,我想为用户加载新的笔尖进行一些编辑。下面是didFinishPickingMediaWithInfo。我可以将图像加载到当前的UIImageView,甚至发送出来和alertview,但即使我尝试加载新的笔尖,它也会回到currentview笔尖并且没有错误。请记住,我是一个完全ios noob,所以如果方法使用错误,请告诉我。提前谢谢。

P.S。只是添加,即使我设法加载新的笔尖,我如何传递前一个笔尖的信息?例如。如果我从nib2中选择一个图像,如何将它传递给nib3?

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info   {
imageOverlay.image = [UIImage imageNamed:@"mask_overlay1.png"];
[imageOverlay release];

// Displaying image to the imageView
imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

// Tried to load a new nib
UIView *currentView = self.view;

// Get the underlying UIWindow, or the view containing the current view.
UIView *theWindow = [currentView superview];


//remove the current view and replace with myView1
[currentView removeFromSuperview];

// set up an animation for the transition between the views
CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

[[theWindow layer] addAnimation:animation forKey:@"createPhotoView"];

}

1 个答案:

答案 0 :(得分:-1)

我不知道您的应用程序的工作流程是什么样的,但如果您需要在选择图像后显示新视图,则应该通过将新视图控制器推送到导航控制器或通过呈现来实现它作为一种模态观点。 这样您就可以轻松返回上一个要求您选择照片的屏幕。

假设您的应用已决定使用UINavigationController

您将使用第一个视图控制器初始化导航控制器作为根视图控制器。

然后,在你的UIImagePicker委托方法中,你会有这样的东西:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    // Initialize your new view controller
    CustomViewController *controller = [[CustomViewController alloc] initWithNibName:@"SecondView" bundle:nil];

    // Pass your custom view controller the image you just selected
    controller.valueToPass = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    [self.navigationController pushViewController:controller animated:YES];

    [controller release];

}

创建一个名为CustomViewController的UIViewController子类,并通过创建可分配的属性(例如我的示例中的valueToPass)传递它。

编辑:

可以只切换视图控制器的可见视图。它就像更改self.view的值一样简单。您可以将两个UIView放在同一个XIB文件中,并将IBOutlets放在控制器中。你可以有一个如下所示的标题:

@interface MyController : UIViewController {

    IBOutlet UIView *view1;

    IBOutlet UIView *view2;

}

// Follow this with properties for all of those IBOutlets as well

然后,在您的控制器代码中:

- (IBAction)switchViews {

    if ([self.view isEqualTo:view1]) {
        self.view = view2;
    } else {
        self.view = view1;
    }

}

这有效,但据我所知,不允许转换。你可以这样做:

- (IBAction)switchViews:(id)sender {

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:NO];

    if ([[secondaryView superview] isEqual:mainView]) {
        [secondaryView removeFromSuperview];
    } else {
        [self.view addSubview:secondaryView];
    }

    [UIView commitAnimations];

}

这两个选项都有效,并允许您使用相同的视图控制器来控制两个视图。但是,如果视图执行不同的任务,则它们应与不同的控制器关联。如果将控制器设置为标题中的属性,则使用不同的控制器类似:

- (IBAction)switchViews:(id)sender {

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:NO];

    if (self.secondaryController) {
        [self.secondaryController.view removeFromSuperview];
        self.secondaryController = nil;
    } else {
        self.secondaryController = [[SecondaryController alloc] initWithNibName:@"SecondView" bundle:nil];
        self.secondaryController.valueToPass = @"I am passing this value!";
        [self.view addSubview:self.secondaryController.view];
    }

    [UIView commitAnimations];

}