【问题标题】:Storyboard passing Images between views故事板在视图之间传递图像
【发布时间】:2013-07-27 16:52:23
【问题描述】:

我正在尝试将图像从一个视图传递到另一个视图。我有一个相机按钮,按下时会触发 uiimagepickercontroller。选择图像后,编辑照片屏幕将被推送到屏幕上。每当屏幕加载时,什么都不会出现。我尝试关注这个帖子Passing image from one view to another,但它似乎不起作用。

这是我的第一个视图控制器的代码:

firstViewController.h

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

- (IBAction)cameraButtonPressed:(id)sender;

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

@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];
}

【问题讨论】:

  • 如果“myImage”是 UIImageView,从 -initWithImage 中删除使用它的代码: - 你的视图没有加载,所以你不能和它对话。单步调试器,看看它是 nil。您实例化的 ECDEditViewController 似乎永远不会出现。而您似乎将一个导航控制器(“composeController”)推到另一个导航控制器上,这充其量是奇怪的,并且可能是错误的。
  • 什么是composeController?您不想将图像传递给 ECDEditViewController 吗?
  • @rdelmar 那是我给它的故事板 ID
  • 你给的ID是什么?你的问题不清楚。 ECDEditViewController 是 UITableViewController 的子类,但您说 composeController 是 UINavigationController。这是两个不同的东西吗?
  • 我的问题与视图控制器无关,它是将图像从一个视图传递到另一个 @rdelmar

标签: iphone ios objective-c ios6 uiimageview


【解决方案1】:

你需要做这样的事情:

- (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]; 

}]; 

}

您的 composeController 实际上是一个 ECDEditViewController,而不是 UINavigationController。所以这一行是不必要的(就像 initWithImage: 方法一样):

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

【讨论】: