在故事板中的两个场景之间传输图像

时间:2013-10-16 05:16:26

标签: ios storyboard

我在主视图控制器中有一个图像,我希望这个图像显示在新的视图控制器中...我知道如何在两个场景之间获取字符串值,但不知道故事板中的图像....请帮助我出来......

Is it right way to pass UIImage to another controller...

if UIImage *img1=[UIImage imageNamed:@"1.jpg"]; and UIImageView is *newimgview  for newViewController

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"imageview"])
    {
        newViewController *nvc=segue.destinationViewController;
        nvc.newimgview.image=img1;

    }
}

4 个答案:

答案 0 :(得分:1)

只需传递UIImage指针,就像对NSString

中的performseguewithidentifier等其他数据一样

答案 1 :(得分:1)

如果您有图像,可以在ViewController中调用它,无需传递它。

[UIImage imageWithContentsOfFile:filePath];

[UIImage imageNamed:filename]

如果你最初没有图像,你可以像指向字符串一样传递它的指针。请阅读此处以获取更多信息:Passing Data between View Controllers

答案 2 :(得分:0)

您可以将文件路径传递到目标视图控制器,或者如果您愿意,您可以真正传递图像。添加此方法并尝试传递图像

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"yourImageTransferSegue"]) {

            YourImageTransferViewController *destViewController = segue.destinationViewController;
            destViewController.imageObject = self.imageToBeTransfered;

    }
}

YourImageTransferViewController的 ViewDidLoad或您想要的任何位置使用图像对象 self.imageObject

答案 3 :(得分:0)

是的......最后我明白了......使用此代码

In case  UIImage *imag1=[UIImage imageNamed:@"some.png"];

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"imageview"])
    {
        newViewController *nvc=segue.destinationViewController;
        nvc.imag2=imag1; //imag2 is UIImage that globally declared in newViewController
    }
}

And in newViewController under ViewDidLoad or wherever u want to use...

self.imageview2.image=imag2;

感谢所有人......