我正在尝试使用2个视图制作iOS应用程序。第一个视图包含两个按钮:Camera和Library。当我选择Library时,我可以访问相机胶卷。我想选择一张照片并在另一个视图中打开它。到现在为止我写了这个函数
-(IBAction) getPhoto:(id) sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if((UIButton *) sender == choosePhotoBtn) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
} else {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self presentViewController:picker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion: Nil];
imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}
女巫在同一视图中打开我的图像。我怎么能在另一个视图中打开这个图像?
答案 0 :(得分:1)
你可以做那样的事情
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[picker dismissViewControllerAnimated:YES completion:^{
YourSeconViewController *svc = [[UIViewController alloc] initWithNibName:@"YourSeconViewController" bundle:nil];
svc.imageView.Image = image;
[self presentViewController:svc animated:YES completion:nil];
}];
}
答案 1 :(得分:0)
我建议的步骤是:
答案 2 :(得分:0)
在同一视图控制器中的另一个视图中显示图像非常简单 - 假设您在viewController中引用了新视图,只需使用:
NewImageView.image = ...
我认为您的意思是将图像传递给另一个视图 controller 。
为此,您应该从imagePickerController:didFinishPickingMediaWithInfo:
方法开始转换到新的viewController。
在新的视图控制器中,您应该声明一个可以设置的公共属性:
@property (monatomic, strong) UIImage* Image;
您可以在转换到新视图控制器之前在新视图控制器上设置该属性,并在新视图控制器的viewWillAppear中使用该属性设置imageView的内容。
如何进行转换取决于应用的设计。例如,您可以使用Segue,在这种情况下,您可以在didFinishPickingMediaWithInfo:
中启动segue并在prepareForSegue
中设置新视图控制器的属性。或者你可以直接创建新的VC:
MyCustomViewController* viewController = [[MyCustomViewController alloc] init];
//pass the image ref to the new view controller here
ViewController.image = image;
//modally present, no navController
[self presentViewController:viewController animated:YES];
//alternatively, push via a nav controller
[self.navigationController pushViewController:viewController animated:YES];