Question

I've had this problem for about 3 hours straight now, and I'm about to throw my Mac across the room.

So basically, I'm trying to pass a UIImage to another view controller. I have the setup so when the user taps one of the UIColectionViewCells, it will send them to another view with a full-screen UIImageView. I just can't seem to figure out how to get the UIImage from ViewController1 to ViewController2.

Here is some of my code. Remember, I'm trying to get this UIImage selectedImage from VC1 to VC2.

collectionView cellForItemAtIndexPath

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = @"GalleryCell";
    GalleryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    [cell.layer setBorderWidth:2.0f];
    [cell.layer setBorderColor:[UIColor whiteColor].CGColor];
    [cell.layer setCornerRadius:5.0f];

    UIImage *usingImage = [imageArray objectAtIndex:indexPath.row];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:usingImage];
    imageView.tag = 100;
    imageView.frame = CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height);

    [cell addSubview:imageView];

    return cell;
}

collectionView didSelectItemAtIndexPath

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *selectedCell = [collectionView cellForItemAtIndexPath:indexPath];
    UIImage *selectedImage = (UIImage *)[selectedCell viewWithTag:100];

    [self performSegueWithIdentifier:@"GotoDetail" sender:nil];
}
Was it helpful?

Solution

Probably the following line is causing this issue:

UIImage *selectedImage = (UIImage *)[selectedCell viewWithTag:100];

When you use viewWithTag: it'll return the UIImageView associated with the cell, not the UIImage.

You need to change it like:

UIImageView *selectedImageView = (UIImageView *)[selectedCell viewWithTag:100];
UIImage *selectedImage = selectedImageView .image;

For passing data, store the selected image in a instance variable (Say selectedImage) and you need to implement the prepareForSegue: like:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{       
        if ([segue.identifier isEqualToString:@"GotoDetail"])
        {
            YourDetailType *detailController = segue.destinationViewController;
            detailController.imageProperty= self.selectedImage;
        }   
}

OTHER TIPS

There are multiple problems with your code:

  1. You should only be adding an image view to the cell if you don't get a call back from the dequeue method. Otherwise each time you recycle a cell you'll add another image view to it, so after a while you'll have dozens of image views on a cell.

  2. Next, you should not use the cell as a place to store your image. You already have an array of images. Use that to pass the image to the other view controller using the indexPath of the selected cell.

That leads to the cause of your crash, which step 2 will fix: You are casting an image view to UIImage type, which is wrong.

Finally, to pass information to your detail view controller, add a property selectedRow (an integer) or selectedRowImage (a UIImageView) to your detail view controller. In your prepareForSegue method, get the destination view controller from the segue, cast it to the correct type, and set the property using the indexPath of the selected cell.

[selectedCell viewWithTag:100] is a UIImageView and not a UIImage, casting will not work.

[(UIImageView *)[selectedCell viewWithTag:100]].image;

You can try getting the UIImage reference of the UIImageView like this.

...But I agree with Duncan C, you should rethink how you are getting the images and not just grab them from the UIImageView of the cell.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top