将图像从一个视图传递到其他iOS

时间:2014-01-05 09:53:54

标签: ios objective-c cocoa-touch uiviewcontroller uiimagepickercontroller

我的应用中有2 UIViewController个。

  1. 第一 viewController中,我在工具栏中有一个UIButton
  2. 当我点击按钮时,它会显示UIActionSheet以选择是从相机还是从图库中拍摄照片。
  3. 当我选择,例如,从图库中拍照并选择一张图片时,它会在第一张viewController中显示图片。
  4. 我想要的是从图库照片或相机拍摄照片,然后转到第二 viewController并在第二 {中显示图片{1}}。

    我搜索了很多并尝试了很多代码,但它不起作用。

    以下是viewController

    的代码
    ViewController.m

    在我的第二个视图中,我创建了一个- (IBAction)PictureButton:(UIButton *)sender { NSLog(@"Click button take picture in toolbar view 1"); UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Photo" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Picture From Camera", @"Picture From Gallery", nil]; [actionSheet setTag:1001]; [actionSheet showInView:self.view]; } -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { switch (buttonIndex) { case 0: { // Take a picture from camera UIImagePickerController * picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentModalViewController:picker animated:YES]; } break; case 1: { // take a picture from gallery photos UIImagePickerController * picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; [self.view addSubview:toolbar]; [self presentModalViewController:picker animated:YES]; } break; } -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissModalViewControllerAnimated:YES]; imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; } ,这是UIImageView

    中的代码
    SecondViewController.h
    你可以帮帮我吗 抱歉我的英语不好,谢谢

1 个答案:

答案 0 :(得分:0)

您似乎想要在FirstViewController选择图片并将其显示在SecondViewContoller中。但是,您没有做任何事情要切换到SecondViewController并将所选图片传递给ViewController.m

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];

    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    secondViewController.imageButton.image = image;
    [self.navigationController pushViewController:secondViewController animated:YES];
 }