将图像从一个视图传递到另一个视图 [英] Passing image from one view to another

查看:86
本文介绍了将图像从一个视图传递到另一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从照片库中获取用户的图片,我想将其传递到另一个视图...

I'm getting an image from the user from the photo gallery, and I want to pass it to a another view...

我读过那个我应该可以简单地做这样的事情:

I've read that I should be able to simply do something like this:

secView.imgView.image = selectedImage.image;

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

Setting my UIImageView on the second view to be the image from my UIImageView... But the UIImageView in the second view is just empty...

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

My code is this...

ViewController.h:

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(仅限相关部分)

ViewController.m (only the relevant parts)

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

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

In another post I read that another approach was to do this:

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

and then in the secondview.h

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

和.m

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

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

But both approaches results in an empty UIImageView... So what is the correct approach to doing this?

提前致谢!

EDIT

SecondView相当简单...(在发布的代码中我尝试并按照方法二)

SecondView is fairly simple at the moment... (in the posted code I try and follow method two)

@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

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

SOLUTION Okay... Went back to the first method and made some alterations which ultimately worked...

-(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中,我创建了UIImage * theImage - 然后我设置了[imgView setImage:theImage];在viewDidLoad中,有效...

And in the secondView, I created UIImage *theImage - then I set [imgView setImage:theImage]; in viewDidLoad, which worked...

推荐答案

这解决了这个问题:

-(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:

SecondView:

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

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

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

@end

以及.m的secondView:

and in the .m of secondView:

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

这篇关于将图像从一个视图传递到另一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆