所以我想要實現的是,當用戶從UICollectionView中點擊一個單元格時,下一個UIView上顯示來自該單元格的圖像。從UICollectionView中的單元格獲取圖像
爲了實現這個,我使用委託方法-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
和NSUserDefaults。這是我正在做它
- 得到電池輸出
- 從細胞的UIImageView的獲取圖像#1
- 將圖像轉換成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;
我有這個事情正在進行,從前一個水龍頭的圖像。如果我第一次點擊圖片(圖片A),我在下一個視圖中什麼都沒有。我回去,然後點擊另一個圖像(圖像B)我得到圖像A從前面的水龍頭 – nupac
可以顯示cellForItemAtIndexPath的實現:方法 – suhit
我很抱歉,對於遲到的響應,但我已經添加了實現 – nupac