【问题标题】:Passing an image from one view to other iOS将图像从一个视图传递到另一个 iOS
【发布时间】:2014-01-22 19:23:48
【问题描述】:

我的应用中有 2 个UIViewControllers。

  1. 1st viewController 中,我在工具栏中有一个 UIButton
  2. 当我点击按钮时,它会显示一个UIActionSheet 来选择是从相机还是从图库中拍照。
  3. 当我选择,例如从图库中拍照并选择一张图片时,它会在第一个viewController中显示图片。

我想要的是从画廊照片或相机中拍照并转到2nd viewController 并在2nd 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 中的代码

@interface SecondViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *imageButton;
@end

你能帮帮我吗。
对不起我的英语不好,谢谢

【问题讨论】:

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


    【解决方案1】:

    您似乎想在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];
     }
    

    【讨论】:

    • 感谢您的回复,我更改了我们的代码,现在它总是返回到第一个视图并且不显示任何图像。
    • 你的意思是imagePicker总是返回FirstView??
    • 当我从画廊照片中挑选一张照片时,它会像以前一样返回到第一个视图,并且不显示我选择的图像。它不去第二个视图的问题总是返回到第一个视图。谢谢
    • 您的 imagePicker 在哪里显示?第一视图还是第二视图? UIImagePickerController 返回到您显示它的位置。
    • 它没有显示在第一个视图中,我无法访问第二个视图,因为我总是返回到第一个视图,这是我项目的链接,我上传了,非常感谢你的帮助 。 mediafire.com/download/y0nwyqg0sdfwe7w/testProject.rar
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    相关资源
    最近更新 更多