Ios 点击UICollectionView从单元格中获取图像

Ios 点击UICollectionView从单元格中获取图像,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,因此,我想要实现的是,当用户点击UICollectionView中的单元格时,该单元格中的图像将显示在下一个UIView中 为了实现这一点,我使用了委托方法-(void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath和NSUserDefaults。我就是这样做的 打开手机 从#1的单元格的UIImageView获取图像 将图像转换为NSData 将数

因此,我想要实现的是,当用户点击UICollectionView中的单元格时,该单元格中的图像将显示在下一个UIView中

为了实现这一点,我使用了委托方法
-(void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath
和NSUserDefaults。我就是这样做的

  • 打开手机
  • 从#1的单元格的UIImageView获取图像
  • 将图像转换为NSData
  • 将数据放入NSUserDefaults
  • 执行到下一个视图控制器的切换
  • 从NSUserDefault获取数据
  • 转换为UIImage并显示在UIImageView中
  • 代码如下:

    -(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;
    }
    
    应该工作,但不是。我得到随机结果。有时在下一个UIView中没有图像,有时有图像但不是我点击的图像

    编辑::以下是
    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;
    
    你为什么这样做:

        NewsfeedCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    
    ?? 您需要获取indexPath的单元格,而不是从池中创建单元格。 使用:


    相反。

    didSelectItemAtIndexPath:
    方法中更改行

    -(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];  
    }
    

    也许您已经解决了这个问题,但我遇到了一个类似的问题,并发现
    UICollectionView
    从0开始

    例如,我的图像被命名为image_1、image_2等等。但是如果我从图像_1开始,第一个单元格中没有任何内容,因此必须从图像_0、图像_1开始,以此类推


    希望这对任何人都有帮助。

    我现在正在做这件事。上一次点击的图像。如果我第一次点击图像(图像A),我在下一个视图中什么也看不到。我返回,然后点击另一个图像(图像B)我从上一个点击中获得图像A您可以显示cellForItemAtIndexPath:Method的实现吗很抱歉响应太晚,但我已经添加了实现
    -(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];  
    }