如何在单击集合视图上的单元格的详细视图上获取相同的图像?

时间:2015-05-14 11:26:42

标签: ios

我正在开发iOS应用程序。我有一个集合视图,它有一些图像。图像来自我创建的数组。当我点击集合视图的单元格时,第二个视图控制器打开。我想要显示我点击集合视图的第二个视图控制器上的特定图像。怎么做?

3 个答案:

答案 0 :(得分:1)

在SecondViewController中创建图像对象,如下所示:

@property (nonatomic, strong) UIImageView *detailImageView;
@property (nonatomic, strong) UIImage *detailImage;

在FirstViewController.m中,执行以下操作:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

 CollectionViewCell *cell = [collectionViewObject dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
 SecondViewController *secondViewController = [[SecondViewController alloc] init];
 secondViewController.detailImage = cell.imageView.image;
 [self.navigationController pushViewController:secondViewController animated:YES];
}

然后在SecondViewController.m viewDidLoad方法

- (void)viewDidLoad
{
 [super viewDidLoad];

  self.detailImageView.image = self.detailImage;
}

希望这对你有用!!

答案 1 :(得分:0)

我希望您在下一个ViewController中创建了UIImageView

第1步。在你的下一个ViewController.h创建图像

@property (nonatomic, strong) UIImage *image;

然后在之前的ViewControler.m中找到你didSelectItemAtIndexPath方法,或者创建它们,并添加下一个代码:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
   MyCollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentificer forIndexPath:indexPath];
   MyViewController *myvc = [[MyViewController alloc] init];
   myvc.image = cell.imageView.image;
   [self presentViewController: myvc animated:YES completion:nil];
}

如果您不使用故事板,请转到第3步

第2步。 如果您使用storyboard 在下一个ViewController.h中创建图片 然后在之前的ViewControler.m中找到你的didSelectItemAtIndexPath方法,或者创建它们,并添加下一个代码:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
   [self performSegueWithIdentifier:@"nextVCSeagueName" sender:self];
}

添加方法

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"nextVCSeagueName"]) {
        MyviewController *myvc = segue.destinationViewController;
        myvc.image = [imagesArray objectAtIndex:[[self.collectionView indexPathsForSelectedItems] firstObject]];
    }
}

第3步。viewDidLoad的下一个ViewController.m中添加

-(void)viewDidLoad {
   [super viewDidLoad];

   myImageView.image = self.image;
}

答案 2 :(得分:0)

使用委托方法知道要传递的图像 -

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

假设您的图片数组名为 imageArray 。在你的类中,声明一个像 -

这样的图像字符串
@propert NSString *selectedImage;

现在,当您实现didSelectItemAtIndexPath方法时,可以将selectedImage变量中该特定行中加载的图像保存。然后,您可以实现prepareForSegue方法将此变量传递给第二个视图控制器。

我们假设您已在 SecondViewcontroller.h 文件中声明了一个名为 imageFromFirstviewController 的变量。

@propert NSString *imageFromFirstviewController;

现在,您可以在 didSelectItemAtIndexPath 方法中执行以下操作 -

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.selectedImage = [self.imageArray objectAtIndex:indexPath.row] ;
}

之后通过segue方法将此图像名称传递给第二视图控制器。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // "my-segue-name" which is assign in the storyboard while attaching the segue
    if ([[segue identifier] isEqualToString:@"my-segue-name"])
    {   
        SecondViewcontroller *secondVC = [segue destinationViewController];
        secondVC.imageFromFirstviewController = self.selectedImage;
    }
} 

现在,由于您可以在第二个视图控制器中访问所选图像,因此无论如何都可以使用此imageFromFirstviewController。

相关问题