如何在ios中将图像从一个页面传递到另一个页面

时间:2016-02-10 12:35:47

标签: ios objective-c

我想通过从图库中选择图像或使用相机将图像从一个页面传递到另一个页面。用户点击显示按钮图像将转移到下一页 我试过这个。但不能在.m文件中工作

 @interface ImagePicker () <UIImagePickerControllerDelegate>
{
    IBOutlet UIImageView *imageview;
    UIImageView *fordisplay;
}
@end

@implementation ImagePicker

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

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)clickOnDate:(id)sender
{
    datePicker *date=[[datePicker alloc]initWithNibName:@"datePicker" bundle:nil];
    [self.navigationController pushViewController:date animated:YES];
}
-(IBAction)selectPhoto:(id)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:picker animated:YES completion:NULL];
}
-(IBAction)takePhoto:(id)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
   DisplayImage *obj_display1=[[DisplayImage alloc]initWithNibName:@"DisplayImage" bundle:nil];

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    //self.imageview.image = chosenImage;
    imageview.image=chosenImage;
     obj_display1.imageDisplayy=chosenImage;**
    [picker dismissViewControllerAnimated:YES completion:NULL];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [picker dismissViewControllerAnimated:YES completion:NULL];

}
-(IBAction)display:(id)sender
{
    DisplayImage *obj_display=[[DisplayImage alloc]initWithNibName:@"DisplayImage" bundle:nil];

    [self.navigationController pushViewController:obj_display animated:YES];
}

并在下一页要显示图像

.h文件

@property(nonatomic,strong) UIImageView *imageDisplayy;

.m文件

@synthesize imageDisplayy;

- (void)viewDidLoad {
    [super viewDidLoad];
    obj_imageview.image=imageDisplayy;


}

3 个答案:

答案 0 :(得分:1)

您正在使用此方法创建DisplayImage实例

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 

执行导航时,您正在创建另一个没有为其设置imageDisplayy属性的新实例。

-(IBAction)display:(id)sender
{
    DisplayImage *obj_display=[[DisplayImage alloc]initWithNibName:@"DisplayImage" bundle:nil];

    [self.navigationController pushViewController:obj_display animated:YES];
}

此问题的一个解决方案是将DisplayImage * obj_display1作为ImagePicker中的属性。

@property(nonatomic,strong) DisplayImage *obj_display1;

在viewDidLoad

中实例化它
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.obj_display1=[[DisplayImage alloc]initWithNibName:@"DisplayImage" bundle:nil];
}

将此方法中的代码更新为

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
//self.imageview.image = chosenImage;
imageview.image=chosenImage;
 self.obj_display1.imageDisplayy=chosenImage;**
[picker dismissViewControllerAnimated:YES completion:NULL];

}

然后执行导航

-(IBAction)display:(id)sender
{
    [self.navigationController pushViewController:self.obj_display1 animated:YES];
}

答案 1 :(得分:0)

你是直接将图像传递给imagview。而不是将图像从一个视图控制器传递到另一个视图。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
   DisplayImage *obj_display1=[[DisplayImage alloc]initWithNibName:@"DisplayImage" bundle:nil];

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    //self.imageview.image = chosenImage;
    imageview.image=chosenImage;
     obj_display1.imageDisplayy=chosenImage;**
    [picker dismissViewControllerAnimated:YES completion:NULL];

}

想要显示图像的下一页

.h文件

@property(nonatomic,strong) UIImage *imageDisplayy;

.m文件

@synthesize imageDisplayy;

- (void)viewDidLoad {
    [super viewDidLoad];
    obj_imageview.image=imageDisplayy;


}

答案 2 :(得分:0)

试试这个:

//In .h file 
// change your property to UIImage instead of imageview

    @property(nonatomic,strong) UIImage *imageDisplayy;