将图像从一个视图传递到另一个视图

时间:2012-02-29 09:38:33

标签: ios uiimageview uiimage uiimagepickercontroller

我从照片库中收到用户的图片,我想把它传递到另一个视图......

我读过我应该能够做到这样的事情:

secView.imgView.image = selectedImage.image;

在第二个视图上将我的UIImageView设置为来自我的UIImageView的图像... 但是第二个视图中的UIImageView只是空的......

我的代码就是这个......

ViewController.h:

@interface ViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
    UIImagePickerController *picker;
    IBOutlet UIImageView *selectedImage;

    SecondView *secondView;
}

@property (nonatomic, retain) UIImageView *selectedImage;
@property (nonatomic, retain) SecondView *secondView;

-(IBAction)sendImage:(id)sender;
-(IBAction)openGallery:(id)sender;

@end

ViewController.m(仅限相关部分)

-(IBAction)sendImage:(id)sender
{
    if(self.secondView == nil)
    {
        SecondView *secView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];

        self.secondView = secView;

        secView.imgView.image = selectedImage.image;
    }

    [self.navigationController pushViewController:secondView animated:YES];
}

-(IBAction)openGallery:(id)sender
{
    picker = [[UIImagePickerController alloc] init];

    picker.delegate = self;

    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentModalViewController:picker animated:YES];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *) Picker 
{    
    [Picker dismissModalViewControllerAnimated:YES];    
}

- (void)imagePickerController:(UIImagePickerController *) Picker

didFinishPickingMediaWithInfo:(NSDictionary *)info 
{    
    // Save the image
    selectedImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];

    [Picker dismissModalViewControllerAnimated:YES];
}

在另一篇文章中,我读到另一种方法是这样做:

-(IBAction)sendImage:(id)sender
{
    if(self.secondView == nil)
    {
        SecondView *secView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];

        self.secondView = secView;

        [secView setImage:selectedImage.image];
    }

    [self.navigationController pushViewController:secondView animated:YES];
}

然后在secondview.h

-(void)setImage:(UIImage *)image;

和.m

-(void)setImage:(UIImage *)image
{
    [imgView setImage:image];
}

但两种方法都会导致空UIImageView ... 那么这样做的正确方法是什么?

提前致谢!

修改

SecondView目前相当简单......(在发布的代码中我尝试并遵循方法二)

@interface SecondView : UIViewController
{
    UIImageView *imgView;
}

@property (nonatomic, retain) IBOutlet UIImageView *imgView;

-(void)setImage:(UIImage *)image;

@end

@implementation SecondView

@synthesize imgView;

-(void)setImage:(UIImage *)image
{
    [imgView setImage:image];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

好的......回到第一种方法并进行了一些最终有效的改动......

-(IBAction)sendImage:(id)sender
{
    if(self.secondView == nil)
    {
        SecondView *secView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];

        self.secondView = secView;

        secView.theImage = selectedImage.image;
    }

    [self.navigationController pushViewController:secondView animated:YES];
}

在第二个视图中,我创建了UIImage * theImage - 然后我设置了[imgView setImage:theImage];在viewDidLoad中,有效...

3 个答案:

答案 0 :(得分:7)

这解决了这个问题:

-(IBAction)sendImage:(id)sender
{
    if(self.secondView == nil)
    {
        SecondView *secView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];

        self.secondView = secView;

        secView.theImage = selectedImage.image;
    }

    [self.navigationController pushViewController:secondView animated:YES];
}

SecondView:

@interface SecondView : UIViewController
{
    UIImageView *imgView;
    UIImage *theImage;
}

@property (nonatomic, retain) IBOutlet UIImageView *imgView;
@property (nonatomic, retain) UIImage *theImage;

-(void)setImage:(UIImage *)image;

@end

并在secondView的.m中:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [imgView setImage:theImage];
}

答案 1 :(得分:0)

我想我发现了问题:

在你的类secondView中,你的UIImageView没有连接到nib文件中的对象UIImageView。

你应该有类似的东西:

IBOutlet UIImageView *imgView;

要检查视图是否正确连接到Nib,您可以执行以下操作:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [imgView setBackgroundColor:[UIColor redColor]];
    }
    return self;
}

你的问题很有意思请告诉我,如果它不起作用我会更深入地检查。

答案 2 :(得分:0)

// First Viewcontroller: - (无效)passimage {

    Second_ViewController *select_main =[[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"Second_ViewController"];
   select_main.tt_image=_bg_imageview.image;
    [[self navigationController]   pushViewController:select_main animated:YES];

}

//第二个Viewcontroller传递Uiimage对象:

  • (void)viewDidLoad {

    _bg_imageview.image = _bb_image; }