从UICollectionView中的单元格中获取图像

时间:2013-11-02 08:10:11

标签: ios objective-c uicollectionview

所以我想要实现的是当用户从UICollectionView点击一个单元格时,来自该单元格的图像显示在下一个UIView上。

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

  1. 获取单元格
  2. 从#1
  3. 获取单元格的UIImageView中的图像
  4. 将图像转换为NSData
  5. 将数据放入NSUserDefaults
  6. 执行segue到下一个视图控制器
  7. 从NSUserDefaults获取数据
  8. 转换为UIImage并在UIImageView中显示。
  9. 以下是代码:

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

3 个答案:

答案 0 :(得分:13)

你为什么这样做:

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

? 您需要获取indexPath的单元格,而不是从池中创建单元格。 使用:

NewsFeedCell *cell = [collectionView cellForItemAtIndexPath:indexPath];

相反。

答案 1 :(得分:2)

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

答案 2 :(得分:0)

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

例如,我的图片被命名为image_1,image_2等。但是,如果我从image_1开始,第一个单元格中就没有任何内容,所以你必须从image_0,image_1开始等等。

希望这有助于任何人。