所以我想要实现的是当用户从 UICollectionView 中点击一个单元格时,来自这个单元格的图像会显示在下一个 UIView 上。
为了实现这一点,我使用了委托方法-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
和 NSUserDefaults。这就是我的做法
- 敲击单元格
- 从 #1 的单元格的 UIImageView 获取图像
- 将图像转换为 NSData
- 将数据放入 NSUserDefaults
- 执行到下一个视图控制器的 segue
- 从 NSUserDefaults 获取数据
- 转换为 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;