nupac
nupac

Reputation: 2489

get Image from cell in a UICollectionView on tap

So what I want to achieve is when a user taps on a cell from the UICollectionView, the image from this cell is displayed on the next UIView.

To implement this I used the delegate method -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath and NSUserDefaults. This is how I am doing it

  1. Get the cell tapped
  2. Get the image from the UIImageView of the cell from #1
  3. Convert the image into NSData
  4. Put the data into NSUserDefaults
  5. Perform the segue to the next view controller
  6. Get the data from the NSUserDefaults
  7. Convert to UIImage and display in a UIImageView.

Here is the code:

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NewsfeedCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    NSData *data = [[NSData alloc]initWithData:UIImagePNGRepresentation(cell.ItemImageView.image)];
    NSLog(@"Before Data %@", data);
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    [def setObject:data forKey:@"feedData"];
    [def synchronize];
    [self performSegueWithIdentifier:@"itemTappedSegue" sender:self];  
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    NSData *data = [def objectForKey:@"feedData"];
    NSLog(@"After Data:%@", data);
    UIImage *image = [[UIImage alloc]initWithData:data];
    self.imageView.image = image;
}

Should work but is not. I get random results. Sometimes there is no image in the next UIView, sometimes there is an image but its not the one I tapped on.

EDIT::here is the implementation for cellForItemAtIndexpath

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
NewsfeedCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
if(response2.count > 0){
    cell.usernameLabel.text = [self getUsername:[response2 objectAtIndex:indexPath.item]];
    [UIApplication sharedApplication].networkActivityIndicatorVisible =YES;
    dispatch_queue_t getUserAvatar = dispatch_queue_create("Avatar downloader", NULL);
    dispatch_queue_t getFeed = dispatch_queue_create("Feed downloader", NULL);
    dispatch_async(getUserAvatar, ^{
        NSString *urlString = [self getAvatarUrl:[response2 objectAtIndex:indexPath.item]];
        NSURL *url = [[NSURL alloc]initWithString:urlString];
        NSData *avatarData = [NSData dataWithContentsOfURL:url];
        dispatch_async(dispatch_get_main_queue(), ^{
            cell.DPImageView.layer.masksToBounds = YES;
            cell.DPImageView.layer.cornerRadius = 6.0f;
            cell.DPImageView.image = [UIImage imageWithData:avatarData];
        });
    });
    dispatch_async(getFeed, ^{
        NSString *URLString = [self getFeedUrl:[response2 objectAtIndex:indexPath.item]];
        NSURL *URL = [[NSURL alloc]initWithString:URLString];
        NSData *feedData = [NSData dataWithContentsOfURL:URL];
        dispatch_async(dispatch_get_main_queue(), ^{
            cell.ItemImageView.image = [UIImage imageWithData:feedData];
            cell.ItemImageView.layer.masksToBounds = YES;
            cell.LikeBtn.hidden = NO;
            cell.CommentBtn.hidden = NO;
            cell.usernameLabel.hidden = NO;
            cell.DPImageView.hidden = NO;
        });
    });
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
else{
    //cell.ItemImageView.image = [UIImage imageNamed:@"NoFeed.png"];
    cell.LikeBtn.hidden = YES;
    cell.CommentBtn.hidden = YES;
    cell.usernameLabel.hidden = YES;
    cell.DPImageView.hidden = YES;
}
return cell;

Upvotes: 2

Views: 10366

Answers (3)

adriennemhenry
adriennemhenry

Reputation: 153

Maybe You've already solved this but I had a similar problem and figured out that UICollectionView starts at 0.

For example my images were named image_1, image_2 and so on. But if I began with image_1 there wouldn't be anything in the first cell so you would have to start with image_0, image_1 and so on..

Hope this helps anyone.

Upvotes: 0

Suhit Patil
Suhit Patil

Reputation: 12023

in didSelectItemAtIndexPath: method change the line

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NewsfeedCell *cell = [collectionView cellForItemAtIndexPath:indexPath]; // put this line in place of creating cell

    NSData *data = [[NSData alloc]initWithData:UIImagePNGRepresentation(cell.ItemImageView.image)];
    NSLog(@"Before Data %@", data);
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    [def setObject:data forKey:@"feedData"];
    [def synchronize];
    [self performSegueWithIdentifier:@"itemTappedSegue" sender:self];  
}

Upvotes: 2

Avi Tsadok
Avi Tsadok

Reputation: 1843

Why you do :

    NewsfeedCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

? You need to get the cell of the indexPath, not create a cell from the pool. Use :

NewsFeedCell *cell = [collectionView cellForItemAtIndexPath:indexPath];

Instead.

Upvotes: 13

Related Questions