问题描述
||
我正在编写一个应用程序,该应用程序的初始视图包含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
但是我该怎么使用呢?我尝试了它然后崩溃了...
解决方法
在ViewController中实现UIImagePickerControllerDelegate。
为此,将“ 2”添加到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将调用该方法。