2011-05-30 114 views
0

我正在编写一个应用程序,该应用程序具有包含2个按钮的初始视图 - 一个允许用户使用相机拍摄照片,另一个允许他选择来自图书馆的图片。从相机/图库中选择一张照片进入不同的视图

我已经编写了允许这种情况发生的代码,但是在选择图片后,我想转到另一个允许分享图片或其他内容的视图。任何人都可以告诉我如何做像“whenPhotoIsSelected,view = newView”?

这是我到目前为止的代码:

#pragma mark - 
-(IBAction) getCameraPicture: (id) sender{ 
UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
picker.delegate = self; 
picker.allowsImageEditing = YES; 
picker.sourceType = (sender == takePictureButton) ? 
UIImagePickerControllerSourceTypeCamera : 
UIImagePickerControllerSourceTypeSavedPhotosAlbum; 
[self presentModalViewController:picker animated:YES]; 

[picker release]; 

}

我知道

-(void) imagePickerController:(UIImagePickerController *)picker 
didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo 

的存在,但我究竟是如何使用它?我试过了,它崩溃了......

+0

imagePickerController:didFinishPickingImage:editingInfo:可能是这里的方法。它为什么会崩溃?你能提供源代码吗? – Sascha 2011-05-30 22:56:48

+0

我修改它为imagePickerController:didFinishPickingImage:editingInfo但我在哪里调用它?这本字典应该包含什么?源代码在我的文章中;我真的不知道该在哪里调用该方法......该代码是ViewController类实现的一部分。 – 2011-05-30 23:00:05

回答

1

在您的ViewController中实现UIImagePickerControllerDelegate。 要做到这一点添加<UIImagePickerControllerDelegate>到你的ViewController的接口声明(在.h文件),像这样:在您的视图控制器的.m文件

@interface YourViewController : UIViewController <UIImagePickerControllerDelegate> { 
// instance variable declarations etc. 
} 

,那就可以实现该方法-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo而不是调用它,就像这样:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo { 
    // add the new view, for example you could push it on your navigation stack if you use a UINavigationController 
} 

当选择图像时,该方法将由UIImagePicker调用。

+0

它工作:)谢谢!我只是推了视图,然后一旦我选择了一张图片就不再挂了。 – 2011-05-30 23:17:20

+0

现在发生的事情是,当我从图书馆中选择图片时,它会进入下一个视图,但是在顶部,我也有“回到相册”,这不应该在那里。你能告诉我如何摆脱它吗? – 2011-05-30 23:39:55

+0

那么如果你使用UINavigationController,你可以在推送新的UIViewController之前发送popViewControllerAnimated:NO,或者你可以通过向UINavigationController发送setNavigationBarHidden:animated隐藏导航条,或者你只需​​要将当前视图设置为新视图self.view = newView)。 – Sascha 2011-05-31 00:00:59

相关问题