【问题标题】:How to Pass Multiple images to other View at a time?如何一次将多个图像传递给其他视图?
【发布时间】:2015-04-07 12:59:07
【问题描述】:

在我的应用程序中,我一次选择五个图像并显示在一个小视图中,如果我单击该按钮,则该视图中的 5 个图像应该转到下一个视图控制器。但是当我单击该按钮时按钮仅第 0 个索引中的图像通过,其余图像未通过。希望有人帮助我

这是我的代码。此代码用于从第一个视图传递到第二个视图,并且 selectedImages 是一个数组,其中保存了所有拾取的图像

SecondViewController *secview=[[SecondViewController   alloc]initWithNibName:@"SecondViewController" bundle:nil];
if (secview.imgView.tag==0) {
secview.img=[self.chosenImages objectAtIndex:0];
}
else if (secview.imgView1.tag==1)
{
secview.img=[self.chosenImages objectAtIndex:1];

}
else if (secview.imgView1.tag==2)
{
secview.img=[self.chosenImages objectAtIndex:2];

}
else if (secview.imgView1.tag==3)
{
secview.img=[self.chosenImages objectAtIndex:3];

}
else if (secview.imgView1.tag==4)
{
secview.img=[self.chosenImages objectAtIndex:4];

}
NSLog(@"%@",secview.img);
[self presentViewController:secview animated:YES completion:nil];

在第二个视图中,我的代码是:

    if (imgView.tag==0)
    {
        imgView.image=img;
    }
    else if (imgView1.tag==1)
    {
        imgView1.image=img;
    }
    else if (imgView2.tag==2)
    {
        imgView2.image=img;
    }
    else if (imgView3.tag==3)
    {
        imgView3.image=img;
    }
    else if (imgView4.tag==4)
    {
        imgView4.image=img;
    }

更新:在 didFinishPickingMedia 我写了这段代码

images = [NSMutableArray arrayWithCapacity:[info count]];
for (NSDictionary *dict in info) {
if ([dict objectForKey:UIImagePickerControllerMediaType] ==   ALAssetTypePhoto){
if ([dict objectForKey:UIImagePickerControllerOriginalImage]){
image=[dict objectForKey:UIImagePickerControllerOriginalImage];
[images addObject:image];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
}

}
else {
NSLog(@"UIImagePickerControllerReferenceURL = %@", dict);
}
}
self.chosenImages = images;
[AllImages setHidden:NO];
previousButtonTag=1000;
scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 4,      self.AllImages.frame.size.width, 104)];
int x =0.5;
for (int i=0; i<5; i++) {
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame=CGRectMake(x, 0, 100, 123);
        button.layer.borderWidth=0.5;
        button.titleLabel.font=[UIFont boldSystemFontOfSize:12];
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        // button.layer.borderColor=[[UIColor lightGrayColor]CGColor];
        button.tag = i;
        [button setBackgroundImage:[self.chosenImages objectAtIndex:i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(OverLayMethod:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [scrollView addSubview:button];
        x += button.frame.size.width;
        x=x+6.0;
    }
    scrollView.contentSize = CGSizeMake(x, scrollView.frame.size.height);
     scrollView.backgroundColor = [UIColor clearColor];
    [self.AllImages addSubview:scrollView];
}

【问题讨论】:

  • 在您的目标视图中再创建一个 UIImage 对象,并使用该对象传递图像。
  • 感谢您的回复@Darshan Kunjadiya ..img 是第二个视图本身的对象

标签: ios uiview uiimageview uibutton


【解决方案1】:

我不知道我收到了您的问题,但我的理解是您想将 5 张图片发送到另一个视图。如果是这样,请在您的 SecondViewController 中定义一个新的 NSArray 属性,例如“images”,并将其添加到您从第一个视图到第二个视图的位置

SecondViewController *secondVC = [SecondViewController new];
[secondVC setImages: self.chosenImages];

您的代码的问题是您正在使用 if/else if 块语句,并且如果 if/else if 中的条件之一评估为 true,则只有您进入块之一。此外, -objectAtIndex: 仅返回一项。

[self.chosenImages objectAtIndex:0];

另外,我确定secview.img 意味着您的SecondViewController 中有一个 UIImage 属性。

【讨论】:

  • 感谢您的回复@user4130613,如果我在其中添加它显示 nil 对象通过
【解决方案2】:

我通过在第二个视图中声明另一个数组解决了我的问题。在第一个视图中,我们将拾取的图像保存在一个数组中。所以第一个视图数组对象 = 第二个视图数组,我在第二个视图中传递了该数组 我在第二个视图中的代码:

for (int i=0; i<[arr count]; i++) {
self.img=[arr objectAtIndex:i];      
if (i==0)
{
imgView.image=img;
}
else if (i==1)
{
imgView1.image=img;
}
else if (i==2)
{
imgView2.image=img;
}
else if (i==3)
{
imgView3.image=img;
}
else if (i==4)
{
imgView4.image=img;
}
}    


}

这样我得到了正确的输出

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多