그래서 내가 원하는 것은 사용자가 UICollectionView에서 셀을 탭하면이 셀의 이미지가 다음 UIView에 표시됩니다.UICollectionView의 셀에서 이미지를 가져옵니다.
이것을 구현하기 위해 대표 메서드 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
과 NSUserDefaults를 사용했습니다. 이것은 내가
- 그 일을하고있는 세포는
- 는
- 은 NSUserDefaults 에 데이터를 넣고있는 NSData로 변환의 # 1
- 에서 셀의있는 UIImageView에서 이미지를 이미지를 가져 오기 탭 가져 오기 방법입니다
- 다음보기 컨트롤러에 연속 재생 수행
- 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;
나는이 일을 지금하고있다. 이전 탭의 이미지. 처음으로 이미지 (이미지 A)를 탭하면 다음보기에서 아무 것도 얻지 못합니다. 다시 돌아가서 다른 이미지 (이미지 B)를 누릅니다. 이전 탭에서 이미지 A를 얻습니다. – nupac
cellForItemAtIndexPath의 구현을 표시 할 수 있습니까? – suhit
늦은 응답으로 미안하지만 구현을 추가했습니다. – nupac