2010-09-10 84 views
0

我是iPhone開發新手。我有兩個名爲pickerviewcontroller和secondviewcontroller的視圖,它們具有不同的xib文件。我通過UIImagePickerController界面從照片庫中選擇一張照片,並試圖在第二個視圖中顯示所選圖像。 pickerController是我的第一個視圖控制器。無法在secondviewcontroller中顯示圖像

pickerController.h文件

#import <UIKit/UIKit.h> 
    #import "SecondViewController.h" 

    @interface pickerControllerViewController : UIViewController<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate> { 
    IBOutlet UIButton *selectpic; 
    UIImageView *imageView; 
    } 
    @property (nonatomic,retain) UIImageView *imageView; 
    @property (nonatomic,retain) UIButton *selectpic; 
    -(IBAction)getpic:(id)sender; 
    //-(void)goNext: (UIImagePickerController *)picker; 
    @end 

pickerController.m呸

#import "pickerControllerViewController.h" 

@implementation pickerControllerViewController 
@synthesize imageView,selectpic; 
-(IBAction)getpic:(id)sender 
{ 
UIImagePickerController *picker = [[UIImagePickerController alloc]init]; 
picker.delegate = self; 
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; 
[self presentModalViewController:picker animated:YES]; 
} 
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 

imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
[[picker parentViewController] dismissModalViewControllerAnimated:YES]; 
SecondViewController *secview = [[SecondViewController alloc]initWithNibName:nil bundle:nil]; 
[secview setImage:imageView]; 

} 

SecondVIewController.h文件

@interface SecondViewController :UIViewController{ 
IBOutlet UIImageView *imageView2; 

} 
-(void)setImage:(UIImage *)image; 
@end 

SecondVIewController.m文件

@implementation SecondViewController 
-(void)setImage:(UIImage *)image{ 
imageView2 = image; 
} 

它沒有顯示任何錯誤。我無法在第二個視圖中顯示圖像。

+0

您的類型都是搞砸了... UIImage和UIImage視圖之間有區別。我想知道爲什麼這不會崩潰所有的時間... – Eiko 2010-09-10 09:51:29

+0

雅我同意。後來我改變了uiimageview到uiimage,並試圖itstill沒有得到。 – 2010-09-13 10:31:48

回答

0

在firstviewcontroller.m

<#import "pickerExampleViewController.h" 

@implementation pickerExampleViewController 
@synthesize selectPic; 
-(IBAction)getpic:(id)sender 
{ 
    UIImagePickerController *picker = [[UIImagePickerController alloc]init]; 
    picker.delegate = self; 
    picker.editing = YES; 
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; 
    [self presentModalViewController:picker animated:YES]; 
    [picker release]; 
} 

#pragma mark imagePickerController delegate methods 

-(void)imagePickerController:(UIImagePickerController *) picker 
     didFinishPickingImage:(UIImage *)image 
       editingInfo:(NSDictionary *)editingInfo 
{ 
    [picker dismissModalViewControllerAnimated:YES]; 
    Second *secview = [[Second alloc] initWithNibName:@"Second" bundle:nil]; 
    secview.view.backgroundColor = [UIColor blackColor]; 
    [secview.imgView setImage:image]; 
    [self.view addSubview:secview.view]; 
    [secview release]; 
} 


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
{ 
    [self dismissModalViewControllerAnimated:YES]; 
}< 

在second.m你必須寫

<#import "Second.h" 

@implementation Second 
@synthesize imgView; 

-(void)setImage:(UIImage *)img 
{ 
    [imgView setImage:img];  
} 
-(IBAction)back 
{ 
    [self.view removeFromSuperview]; 
} 

-(void)dealloc 
{ 
    [imgView release]; 
    [super dealloc]; 
} 
@end 
< 
-1

創建pickerControlle A類NSData的變量,並存儲在NSData的圖像數據

再通過NSData的要下一個類

+0

我已經採取了NSData類型,並將圖像存儲在其中,並將數據傳遞給第二視圖控制器中的setImage方法。但我並沒有得到。 – 2010-09-13 10:33:45