无法在secondviewcontroller中显示图像

时间:2010-09-10 09:34:26

标签: iphone uiimagepickercontroller

我是iPhone开发的新手。我有两个名为pickerviewcontroller和secondviewcontroller的视图,它们有不同的xib文件。我通过照片库中的UIImagePickerController接口选择一张图片,我试图在第二个视图中显示所选图像。 pickerController是我的第一个视图控制器。

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 fie

#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;
}

没有显示任何错误。我无法在第二个视图中显示图像。

2 个答案:

答案 0 :(得分: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 :(得分:-1)

在pickerControlle类中创建一个NSData变量 并将图像数据存储在NSData中

然后将NSData传递给下一个类