故事板在视图之间传递图像

时间:2013-07-27 16:52:23

标签: iphone ios objective-c ios6 uiimageview

我正在尝试将图像从一个视图传递到另一个视图。我有一个相机按钮,当按下时会触发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];
}

enter image description here

1 个答案:

答案 0 :(得分:3)

你需要做这样的事情:

- (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];
相关问题