从相机/库中选择一张照片后,转到不同的视图

时间:2011-05-30 22:51:55

标签: iphone objective-c ios4

我正在编写一个包含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

但我究竟如何使用它?我试过了它就崩溃了......

1 个答案:

答案 0 :(得分:1)

在ViewController中实现UIImagePickerControllerDelegate。 为此,将<UIImagePickerControllerDelegate>添加到ViewController的接口声明(在.h文件中),如下所示:

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

然后在ViewController的.m文件中实际实现方法-(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将调用该方法。