跨视图控制器传输图像

时间:2011-07-27 13:03:19

标签: iphone objective-c ios xcode imageview

在我的iPhone应用程序上,我有两个viewControllers(firstViewController,secondViewController),在firstViewController上,用户可以从相机胶卷中选择一张照片,然后将其显示在imageView中,但是我还需要在secondViewController中显示它但是有不知道怎么做。

您是否也可以深入解释您的答案,因为我对Objective-C

相当新

这是我的代码:

firstViewController.h

#import <UIKit/UIkit.h>

@interface firstViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
    UIImageView *theImageView;
}

@property (nonatomic, retain) IBOutlet UIImageView *theImageView;
-(IBAction)selectExistingPicture;

@end

firstViewController.m

#import "firstViewController.h"
#import "secondViewController.h"

@implementation firstViewController

@synthesize theImageView

-(IBAction) selectExistingPicture
{
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentModalViewController:picker animated:YES];
        [picker release];
    }
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage : (UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    theImageView.image = image;
    [picker dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)  picker
{
    [picker dismissModalViewControllerAnimated:YES];
}

-(IBAction)switchSecondViewController {

    SecondViewController *viewcontroller = [[SecondViewController alloc]     initWithNibName:@"SecondViewController" bundle:nil];
    [self presentModalViewController:viewcontroller animated:YES];
    [viewcontroller release];
}
// Default Apple code

@end

在secondViewController中没有多少,所以我不打算发布该代码。

2 个答案:

答案 0 :(得分:0)

在第二个视图控制器中使用

类型的方法
-(void)setImage:(UIImage *)image

在创建viewcontroller之后的下面的方法中,调用带有图像的方法

-(IBAction)switchSecondViewController {
    SecondViewController *viewcontroller = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    [self presentModalViewController:viewcontroller animated:YES];
    [viewcontroller setImage:theImageView.image]
    [viewcontroller release];
}

答案 1 :(得分:0)

你需要在第二个视图中声明一个属性,这样你就可以从第一个视图中设置一个像这样的东西:

secondImageView是secondView中的属性,你必须设置它

-(IBAction)switchSecondViewController {

    SecondViewController *viewcontroller = [[SecondViewController alloc]     initWithNibName:@"SecondViewController" bundle:nil];
     viewcontroller.secondImageView.image = firstViewImage;
    [self presentModalViewController:viewcontroller animated:YES];
    [viewcontroller release];
}