Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 458
nupac Avatar asked Jan 28 '26 02:01

nupac


2 Answers

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.

like image 63
Avi Tsadok Avatar answered Jan 29 '26 20:01

Avi Tsadok


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];  
}
like image 20
Suhit Patil Avatar answered Jan 29 '26 20:01

Suhit Patil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!