简体   繁体   English

情节提要在视图之间传递图像

[英]Storyboard passing Images between views

I'm trying to pass an image from one view to another. 我正在尝试将图像从一个视图传递到另一个视图。 I have a camera button and when press fires off the uiimagepickercontroller. 我有一个相机按钮,当按下时会触发uiimagepickercontroller。 Once the image is selected the edit photo screen is pushed onto the screen. 选择图像后,编辑照片屏幕将被推到屏幕上。 Whenever the screen loads nothing appears. 只要屏幕加载,就不会显示任何内容。 I tried following this post Passing image from one view to another but it seems not to work. 我尝试按照这篇文章将图像从一个视图传递到另一个视图,但是似乎不起作用。

Here is my code for the first view controller: 这是我的第一个视图控制器的代码:

firstViewController.h firstViewController.h

@interface firstViewController : UITableViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate>

- (IBAction)cameraButtonPressed:(id)sender;

firstViewController.m firstViewController.m

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    [self dismissViewControllerAnimated:YES completion:^{

        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
        ECDEditViewController *viewController = [[ECDEditViewController alloc] initWithImage:image];

        UINavigationController *composeController = [self.storyboard instantiateViewControllerWithIdentifier:@"showCompose"];
        //[self.navigationController presentViewController:composeController animated:YES completion:nil];
        [self.navigationController pushViewController:composeController animated:YES];

    }];

}

secondViewController.h secondViewController.h

@interface ECDEditViewController : UITableViewController <UIImagePickerControllerDelegate>

- (id)initWithImage:(UIImage *)aImage;

@property (strong, nonatomic) IBOutlet UIImageView *myImage;
@property (nonatomic, strong) UIImage *image;

secondViewController.m

@implementation ECDEditViewController
@synthesize myImage;
@synthesize image;


- (id)initWithImage:(UIImage *)aImage {

    self = [super init];
    if (self) {
        if (!aImage) {
            NSLog(@"sorry no picture loaded®");
            return nil;
        }
        self.image = aImage;
        [myImage setImage:aImage];
        NSLog(@"picture loaded");
   }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"im trying");

    [myImage setImage:image];
    [myImage setContentMode:UIViewContentModeScaleAspectFit];
}

在此处输入图片说明

You need to do something like this: 您需要执行以下操作:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 

[self dismissViewControllerAnimated:YES completion:^{ 

UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; 

ECDEditViewController *composeController = [self.storyboard instantiateViewControllerWithIdentifier:@"showCompose"]; 
composeController.image = image; 
[self.navigationController pushViewController:composeController animated:YES]; 

}]; 

}

Your composeController is actually an ECDEditViewController, not a UINavigationController. 您的composeController实际上是ECDEditViewController,而不是UINavigationController。 So this line is unnecessary (as is the initWithImage: method): 因此,这一行是不必要的(initWithImage:方法也是如此):

 ECDEditViewController *viewController = [[ECDEditViewController alloc] initWithImage:image];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM