2011-07-27 92 views
1

在我的iPhone應用程序上,我有兩個viewController(firstViewController,secondViewController),firstViewController用戶可以從相機膠捲中選擇一張照片,然後將其顯示在一個imageView中,但是我還需要顯示它在secondViewController中,但不知道如何。跨視圖控制器傳輸圖像

你能不能也請解釋深入你的答案,因爲我是相當新的目標-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沒有太多,所以我不打擾發佈該代碼。

+0

如果您的圖像存儲在應用程序的「文檔目錄」中,則可以將圖像名稱傳遞給下一個類,您可以在其中使用它來顯示圖像。 – rptwsthi

回答

0

在類型

-(void)setImage:(UIImage *)image 

在下面的方法,你的第二個視圖控制器的方法創建一個視圖控制器調用該方法後的圖像,如圖

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

您需要聲明在第二個視圖中的屬性,所以你可以從你的第一個視圖設置圖像這樣的一些事情:

secondImageView是secondView中的屬性,你必須設置它

-(IBAction)switchSecondViewController { 

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

除了firstViewImage部分,這很好,我認爲我應該把原始圖像放在那裏,但我所擁有的是UIImageView(theImageView),它給我一個錯誤,因爲它不是UIImage,我應該放在那裏? –

+0

viewcontroller.secondImageView.image = firstViewImage.image; – Maulik

+0

現在,當我在第二個控制器 –

相關問題